jQuery,只有在单击li而不是a标记时才会触发

jQuery,只有在单击li而不是a标记时才会触发,jquery,Jquery,我有以下设置 <ul> <li><a href="http://www.example.com">Example</a></li> <li><a href="http://www.example2.com">Example2</a></li> </ul> li的宽度由css设置为400px 我正在尝试编写一些jquery,它只在单击li而不是a标记时才会

我有以下设置

<ul>
   <li><a href="http://www.example.com">Example</a></li>
   <li><a href="http://www.example2.com">Example2</a></li>
</ul>
li的宽度由css设置为400px

我正在尝试编写一些jquery,它只在单击li而不是a标记时才会触发

建议?

在子元素上使用:

$('li').click(function(){
   alert('This will be alerted on the li, not the anchor inside');
});

$('li a').click(function(event){
   event.stopPropagation();
});


在单击处理程序中获取事件时,请查看event.target/event.originalTarget

这将指示是哪个元素导致了事件,您可以处理它,也可以忽略它

function clickHanlder (e)
{
   if (e.target.tagName != "A") { return; }
}
您还可以在函数中输入“e”,然后检查目标。只要看看多莉的评论,我会同意的。在我看来,这是最好的路线,而且总是有效的

$('element').on('click', function (e) {
    if(e.target == 'check the tag here'){
       // do something.
    }
});
演示
-->


@Dom也可以,但我认为stopPropagation()在这个用例中工作得很好
$('li a').click(function(event){
   return false;
});
$('li').click(function(){
alert('li here');
});
$('ul li a').click(function(){
return false;
});