对于每个函数jquery

时间:2011-09-07 15:24:27

标签: javascript jquery

我有一个切换功能,我工作正常,只是我不能让它适用于页面上的每个元素。

我目前的职能是

$(".tog").click(function() {
                $("#shortinfo").toggle();
                $('#shortinfo').toggleClass('open');
                return false;
            })

我试过

$(".tog").each.click(function() {
                $("#shortinfo").toggle();
                $('#shortinfo').toggleClass('open');
                return false;
            })

$(".tog").each (click(function() {
                $("#shortinfo").toggle();
                $('#shortinfo').toggleClass('open');
                return false;

我也有类似的手风琴

<h1>title </h1>
<div class="shortcontent">asdasdasd</div>
<div class="longcopy">long stuff here</div>

<h1>title </h1>
<div class="shortcontent">asdasdasd</div>
<div class="longcopy">long stuff here</div>

<h1>title </h1>
<div class="shortcontent">asdasdasd</div>
<div class="longcopy">long stuff here</div>

<h1>title </h1>
<div class="shortcontent">asdasdasd</div>
<div class="longcopy">long stuff here</div>

这个想法是当它点击的'短缺'消失时。目前只发生在第一部分

1 个答案:

答案 0 :(得分:1)

现在问题已经改变......

你使用了错误的选择器。首先,#shortinfo.shortcontent不同。其次,为了确保只有正确的.shortcontent消失,您需要使用.siblings()

如果.tog元素在每个手风琴中......

$(".tog").click(function() {
                var $shortContent = $(this).siblings(".shortcontent");
                $shortContent.toggle();
                $shortContent.toggleClass('open');
                return false;
            });

如果有一个.tog元素,并且您希望所有.shortcontent崩溃...

$(".tog").click(function() {
                var $shortContent = $(".shortcontent");
                $shortContent.toggle();
                $shortContent.toggleClass('open');
                return false;
            });

这应该足以让你开始。

相关问题