affect only the clicked element in jquery for multiple elements with same class

情到浓时终转凉″ 提交于 2019-12-07 09:20:55

问题


i've been looking all over for a solution to this but i just can't figure out how.

what i want is for the specific header that was clicked to be the only one to expand.

demo is here: http://jsfiddle.net/bm92L0eg/1/

html:

<h3 class="click-list">header a</h3>
<ul class="open-list">
    <li>item</li>
    <li>item</li>
    <li>item</li>
</ul>

<h3 class="click-list">header b</h3>
<ul class="open-list">
    <li>item</li>
    <li>item</li>
    <li>item</li>
</ul>

<h3 class="click-list">header c</h3>
<ul class="open-list">
    <li>item</li>
    <li>item</li>
    <li>item</li>
</ul>

jquery:

$('.click-list').click(function() {
    $(this).find('.open-list').slideToggle(600);
});

that jquery code didn't work while this code below is the one that acivates all headers:

$('.open-list').slideToggle(600);

回答1:


Use .next() to select the adjacent .open-list element.

It should be:

$('.click-list').click(function () {
    $(this).next('.open-list').slideToggle(600);
});

Updated Example



来源:https://stackoverflow.com/questions/28377133/affect-only-the-clicked-element-in-jquery-for-multiple-elements-with-same-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!