今天早上我是一个 jQuery 新手,这是我想出的代码,用于显示一个 div,该 div 伴随着调用它的 anchor ,在一个带有“foo”类的 div 内。它不起作用:P
$('div.foo').children('a').click(function(event){
event.preventDefault();
if ($(this).closest('div').('div').is(':hidden')) {
$(this).closest('div').('div').show("slow");
} else {
$(this).closest('div').('div').hide("slow");
}
});
HTML:
<div class="foo">
<a href="#" title="">Click me!</a>
<div>And this will appear!</div>
</div>
我希望能够有多个相同的 foo div(当然除了嵌套 div 的实际内容),我所需要做的就是为包含的 div 分配“foo”类,以便导致包含 anchor 以在单击时显示包含的 div。
这种事情可能吗?预先感谢您的回复。
最佳答案
试试这个
$('div.foo a').click(function(event){
event.preventDefault();
$(this).next().toggle('slow');
});
关于jquery - 如何使 jQuery 单击事件仅影响嵌套在同一父元素中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1359838/