JQuery 在点击 li 时触发点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9302367/
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
JQuery fire click event on clicking li
提问by Dheeraj Agrawal
I have ul li list and I want to fire a jQuery click event on clicking "li" element but except the first li/first-child. So suppose if I have a below list:
我有 ul li 列表,我想在单击“li”元素时触发 jQuery 单击事件,但第一个 li/first-child 除外。所以假设我有以下列表:
<script type="text/javascript">
$(document).ready(function(){
$(".sample_list li").click(function(){
alert("hello");
});
});
</script>
<ul class="sample_list">
<li>First Child</li>
<li>Second Child</li>
<li>Third Child</li>
<li>Fourth Child</li>
</ul>
Now when I click other li's except the first one I should get the alert box. I think I have to write something in this $(".sample_list li"), something like $(".sample_list li:other-child")but not sure. Please help
现在,当我单击除第一个之外的其他 li 时,我应该会看到警告框。我想我必须在这个$(".sample_list li") 中写一些东西,比如$(".sample_list li:other-child")但不确定。请帮忙
Thanks in advance
提前致谢
回答by Rafay
回答by Selvakumar Arumugam
You can do it in many ways, one of such is .notor :not.. See below,
您可以通过多种方式做到这一点,其中之一是.not或:not.. 见下文,
$(document).ready(function(){
$(".sample_list li:not(:eq(0))").click(function(){
alert("hello");
});
});
or
或者
$(document).ready(function(){
$(".sample_list li").not(':eq(0)').click(function(){
alert("hello");
});
});
回答by Selvakumar Arumugam
Copying directly from JQuery selector for table cells except first/last row/column, what about
直接从JQuery 选择器复制表格单元格,除了第一行/最后一行/列,怎么样
$('ul li:not(:first-child)')
Haven't tried it but it looks like you're trying to do the same thing.
还没有尝试过,但看起来您正在尝试做同样的事情。
回答by crush
<script type="text/javascript">
$(document).ready(function(){
$(".sample_list li").click(function(){
if (this.previousSibling != null)
alert("hello");
});
});
</script>