简体   繁体   English

jQuery切换多个元素

[英]jQuery toggle multiple elements

I can not make this piece of code work: 我无法使这段代码工作:

$("a.expand_all").on("click",function(){
    $(this).text('Hide');
    $('.answer').each(function () {
        $(this).slideDown(150, function () {
            $(this).show();
        });
    });
}, function () {
    $(this).text('Expand');
    $('.answer').each(function () {
        $(this).slideUp(150, function () {
            $(this).hide();
        });
    });
});

I'm trying to collapse expend multiple divs, but nothing happens on click event. 我试图折叠花费多个div,但点击事件没有任何反应。 I 'm using latest jQuery 1.10.1 我正在使用最新的jQuery 1.10.1

That doesn't look like valid event binding to me, having the two functions there. 这看起来不像是绑定到我的有效事件,在那里有两个函数。

HTML - I added a div wrapper for event delegation HTML - 我为事件委托添加了一个div包装器

<div class="expando_content">
<a class="expand_all" href="#">Expand</a>

    <p class="answer">I'm an answer!</a>
        <p class="answer">Another answer</a>
            <p>Dynamically added "expand more" goes below...it won't work :(</p>
            <div id="thing"></div>
        </p>
    </p>
</div>

JS - moved the toggling functionality inside one function. JS - 在一个函数中移动了切换功能。

$(".expando_content").on("click", ".expand_all", function () {

    if (!$('.answer').is(':visible')) {
        $(this).text('Hide');
        $('.answer').each(function () {
            $(this).slideDown(150, function () {
                $(this).show();
            });
        });
    } else {
        $(this).text('Expand');
        $('.answer').each(function () {
            $(this).slideUp(150, function () {
                $(this).hide();
            });
        });
    }
});

$('<a class="expand_all" href="#">expand more</a>').appendTo($('#thing'));

jsFiddle 的jsfiddle

It looks to me like you're using jQuery's .on method incorrectly. 它看起来像你正在使用jQuery的.on方法错误。 That method has some overloads, but none of them (sensibly) takes two functions. 该方法有一些重载,但它们(明智地)都没有两个函数。

If I understand what you're trying to do correctly, you just want to toggle some answer elements when a <a> tag is clicked. 如果我理解您正在尝试正确执行的操作,则只需在单击<a>标记时切换一些答案元素。 What you really need to do is have some way of determining if your answers are expanded or not. 你真正需要做的是确定你的答案是否得到扩展。 There are multiple ways to do that, but I've chosen to use a data element: 有多种方法可以做到这一点,但我选择使用数据元素:

<a class="expand_all" href="#" data-collapsed="true">expand</a>
<p class="answer">I'm an answer!</a>
<p class="answer">Another answer</a>

Then your JavaScript can be simplified thusly: 那么你的JavaScript可以简化:

$('a.expand_all').on("click",function(){
    if( $(this).data('collapsed') ) {
        $(this).text('hide').data('collapsed','');
        $('.answer').slideDown(150);
    } else {
        $(this).text('expand').data('collapsed','true');
        $('.answer').slideUp(150);
    }
});

I simplified some of your constructs as well. 我也简化了你的一些构造。 In particular, in your code: 特别是在你的代码中:

$('.answer').each(function () {
    $(this).slideDown(150, function () {
        $(this).show();
    });
});

The .each is unnecessary. .each是不必要的。 Just applying a jQuery method is essentially equivalent to calling .each . 仅应用jQuery方法基本上等同于调用.each You rarely need to use .each . 你很少需要使用.each So that simplifies to this: 这简化了这个:

$('.answer').slideDown(150, function () {
    $(this).show();
});

Then, .slideDown shows the element before it starts, so there's no need to call .show a second time. 然后, .slideDown在它开始之前显示元素,因此不需要再次调用.show So we can get rid of the callback, simplifying all of this to: 所以我们可以摆脱回调,将所有这些简化为:

$('.answer').slideDown(150);

You can see all of this in action here: 你可以在这里看到所有这些:

http://jsfiddle.net/Jammerwoch/sRnkw/5/ http://jsfiddle.net/Jammerwoch/sRnkw/5/

Lastly, the reason I asked whether any of your elements are dynamically added is because if they are, the way you are attaching them won't work. 最后,我问你是否动态添加任何元素的原因是因为如果它们是,你附加它们的方式将无法工作。 That is, the jQuery selectors run once, and then don't get re-run when you add new elements. 也就是说,jQuery选择器运行一次,然后在添加新元素时不再重新运行。 So you have to be more clever. 所以你必须更聪明。 That's described in the jsfiddle above. 这是在上面的jsfiddle中描述的。 Let me know if you need more clarification on that point. 如果您需要更多关于这一点的说明,请与我们联系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM