给定以下无序列表:
<ul class="nb">
<li class="home"><a href="index.html" class="current"><span class="displace">Home</span></a></li>
<li class="products"><a href="products.html" title="Products"><span class="displace">Products</span></a></li>
<li class="services"><a href="services.html" title="Services"><span class="displace">Services</span></a></li>
<li class="support"><a href="support.html" title="Support"><span class="displace">Support</span></a></li>
<li class="company"><a href="company.html" title="Company"><span class="displace">Company</span></a></li>
<li class="contact"><a href="contact.html" title="Contact"><span class="displace">Contact</span></a></li>
</ul>
您能告诉我为什么以下代码只选择第一个列表项吗:
var status = 'closed';
$('ul.nb li a').click(function(e){
e.preventDefault();
if ($status == 'closed'){
var li = $(this).closest('li');
var items = li.parent().children();
console.log(items.html());
}
});
期望结果是选择所有列表项,控制台显示
<a href="index.html" class="current"><span class="displace">主页</span></a>
谢谢!
li.parent()
是否按照你的预期执行了吗?另外,你为什么不直接使用var items = $(this).closest('ul').children();
呢? - zkcro