javascript - jquery onclick 展开 div 并更改 span 内的文本

标签 javascript jquery html css

我的脚本有问题,我单击 H3,无论我是否单击第二个 H3,它都会展开并且只更改 span 中的第一个文本。我想折叠它后,它不会变回原始文本。 我应该用什么?质量

   <script type="text/javascript">
    jQuery(document).ready(function() {
      jQuery(".pad").hide();

      //toggle the componenet with class msg_body
      jQuery(".heading").click(function()
      {
        jQuery(this).next(".pad").slideToggle(500);
        jQuery('#span1).html('&#8743;')
      });
    });
    </script>

<h3 class="heading">text1<span id="span1" style="float:right;">&#8744;</span></h3>
<div id="occ" class="pad">
content
</div>
<h3 class="heading">Text2<span id="span1" style="float:right;">&#8744;</span></h3>

<div id="tech" class="pad">
content
</div>

最佳答案

您需要为箭头设置切换功能。因为这不能用你需要使用 id 的 html 代码来完成,所以你可以专门针对它们,这里是你需要的 jquery:

$(document).ready(function () {
    $('.pad').hide();
    $('.heading').click(function () {
        $(this).next('.pad').slideToggle();
        if($(this).find('.span1').attr('id') == 'yes') {
            $(this).find('.span1').attr('id', '').html('&#8744;');
        } else {
            $(this).find('.span1').attr('id', 'yes').html('&#8743;');
        }
    });
});

fiddle 演示:

http://jsfiddle.net/Hive7/5xueU/2/

关于javascript - jquery onclick 展开 div 并更改 span 内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20858962/

相关文章:

javascript附加到文本文件忽略元素

html - 仅使用 CSS 的父 div 的动态大小动画

html - 导航栏中的中心链接和按钮

javascript - 选择新表时删除以前的表

javascript - Arbor Js - 节点点击?

javascript - 如何在ng-repeat下拉输入框中添加占位符?

javascript - jQuery水平滑动选项卡机制

javascript - 使用 Javascript 发送邮件,无需通过 Outlook

jquery - 如何找到 td 中单击的下一行按钮?

javascript - 如何在父div下的所有div中写入内容?