jQuery 选择正确的 DOM 元素
实例:
我有一些无序列表集(菜单+产品列表)。
我的排序脚本工作正常,但是当我尝试在一页插入两个列表时 - 它们没有作为单独的列表工作,而是都被排序。我不希望这样,我希望一个菜单仅对下面的第一个列表进行排序,而不是对两者进行排序。
我一直在努力尝试 (this).next() 等,但没有任何帮助。我无法更改 DOM,只能编辑 jQuery 代码。
有什么想法吗?
HTML:
<ul class="menu">
<li><a data-value="all" href="#">All</a></li>
<li><a data-value="big" href="#">Big</a></li>
<li><a data-value="small" href="#">Small</a></li>
</ul>
<ul class="list">
<li><div data-value="big">BIG</div></li>
<li><div data-value="small">SMALL</div></li>
<li><div data-value="big">BIG</div></li>
<li><div data-value="small">SMALL</div></li>
<li><div data-value="big">BIG</div></li>
<li><div data-value="big">BIG</div></li>
</ul>
<ul class="menu">
<li><a data-value="all" href="#">All</a></li>
<li><a data-value="big" href="#">Big</a></li>
<li><a data-value="small" href="#">Small</a></li>
</ul>
<ul class="list">
<li><div data-value="big">BIG</div></li>
<li><div data-value="small">SMALL</div></li>
<li><div data-value="big">BIG</div></li>
<li><div data-value="small">SMALL</div></li>
<li><div data-value="big">BIG</div></li>
<li><div data-value="big">BIG</div></li>
</ul>
jQuery:
jQuery('ul.menu li a[data-value=all]').click(function(e) {
jQuery('.list li div').show();
e.preventDefault();
});
jQuery('ul.menu li a[data-value=big]').click(function(e) {
jQuery('.list li div').show(); //I know this looks just BAD, but that's just for jsfiddle
jQuery('.list li div[data-value=small]').hide();
e.preventDefault();
});
jQuery('ul.menu li a[data-value=small]').click(function(e) {
jQuery('.list li div').show(); //I know this looks just BAD, but that's just for jsfiddle
jQuery('.list li div[data-value=big]').hide();
e.preventDefault();
});
实例:
Live example:
I have a few sets of unordered lists (menu + product list).
My sorting script works fine, but when I try to insert two lists at one page - they are not working as separate lists and are being both sorted. And I don't want that, I want one menu to sort only the first list below, not both of them.
I've been trying hard with (this).next() etc., but nothing helps. I'm unable to change the DOM, can only edit jQuery code.
Any ideas?
HTML:
<ul class="menu">
<li><a data-value="all" href="#">All</a></li>
<li><a data-value="big" href="#">Big</a></li>
<li><a data-value="small" href="#">Small</a></li>
</ul>
<ul class="list">
<li><div data-value="big">BIG</div></li>
<li><div data-value="small">SMALL</div></li>
<li><div data-value="big">BIG</div></li>
<li><div data-value="small">SMALL</div></li>
<li><div data-value="big">BIG</div></li>
<li><div data-value="big">BIG</div></li>
</ul>
<ul class="menu">
<li><a data-value="all" href="#">All</a></li>
<li><a data-value="big" href="#">Big</a></li>
<li><a data-value="small" href="#">Small</a></li>
</ul>
<ul class="list">
<li><div data-value="big">BIG</div></li>
<li><div data-value="small">SMALL</div></li>
<li><div data-value="big">BIG</div></li>
<li><div data-value="small">SMALL</div></li>
<li><div data-value="big">BIG</div></li>
<li><div data-value="big">BIG</div></li>
</ul>
jQuery:
jQuery('ul.menu li a[data-value=all]').click(function(e) {
jQuery('.list li div').show();
e.preventDefault();
});
jQuery('ul.menu li a[data-value=big]').click(function(e) {
jQuery('.list li div').show(); //I know this looks just BAD, but that's just for jsfiddle
jQuery('.list li div[data-value=small]').hide();
e.preventDefault();
});
jQuery('ul.menu li a[data-value=small]').click(function(e) {
jQuery('.list li div').show(); //I know this looks just BAD, but that's just for jsfiddle
jQuery('.list li div[data-value=big]').hide();
e.preventDefault();
});
Live example:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这就是您正在寻找的结果: http://jsfiddle.net/KezWE/4/
让我知道!
I think this is the result you are looking for: http://jsfiddle.net/KezWE/4/
Let me know!
你能给你的 ul 一个唯一的 id 并明确引用它们 - 使用“.menu”将影响两个菜单 - 这就是它应该如何工作。
Can you give your ul's a unique id and reference them explicitly - using ".menu" will affect both menus - that is how it is supposed to work.