如何使用 next() 在 jquery 中找到最近的 ul?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10304019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use next() to find the closest ul in jquery?
提问by Patrioticcow
i have a simple menu, something like this:
我有一个简单的菜单,像这样:
<ul id="navigation">
<li id="m_li">
<div><span>+</span><a href="#" >myself</a></div>
<ul class="test" id="x_ul">
<li id="li">
<a href="#" >Pictures</a>
</li>
<li id="li">
<a href="#" >Audio</a>
</li>
</ul>
</li>
<li id="profile_modeling_li">
<div><span>+</span><a href="#" >friend</a></div>
<ul class="test" id="y_ul">
<li id="li">
<a href="#" >Pictures</a>
</li>
<li id="li">
<a href="#" >Video</a>
</li>
</ul>
</li>
</ul>
then i use jquery so when i click on the +
sign the ul slides down:
然后我使用 jquery,所以当我点击+
标志时,ul 会向下滑动:
$("#navigation > li > div > span").click(function(){
if(false == $(this).next('ul').is(':visible')) {
$('#accordion ul').slideUp(300);
}
$(this).next('ul').slideToggle(300);
});
the issue is that $(this).next('ul')
doesn't seem to fond the next ul
问题是它$(this).next('ul')
似乎不喜欢下一个 ul
any ideas what am i doing wrong?
任何想法我做错了什么?
回答by gion_13
use
用
$(this).parent().next()
instead of
代替
$(this).next();
and, of course, you can pass a selector (more simple or more complex) that identifies the desired element as argument:
当然,您可以传递一个选择器(更简单或更复杂),将所需元素标识为参数:
$(this).parent().next('ul.test[id$="_ul]"');
回答by Selvakumar Arumugam
.next / .prev
traverse through siblings and ul is not a children of the span. You should probably find closest li
and find .test
.
.next / .prev
遍历兄弟姐妹和 ul 不是跨度的孩子。您可能应该找到最接近的li
并找到.test
.
$(this).closest('li').find('ul.test')
回答by Hüseyin BABAL
Convert
转变
$(this).next('ul')
to
到
$(this).parent().next('ul')
You can see here http://jsfiddle.net/PYqS2/
你可以在这里看到http://jsfiddle.net/PYqS2/