JQuery.click()选择器忽略<;a>;子元素?

JQuery.click()选择器忽略<;a>;子元素?,jquery,Jquery,我需要在用户单击元素或其中的任何内容时触发事件,除非他单击 我如何才能做到这一点?您可以使用: $("div").click(function (e) { if (e.target.nodeName.toLowerCase() != 'a') alert("hello world"); }); 您可以使用: $("div").click(function (e) { if (e.target.nodeName.toLowerCase() != 'a') alert("hello

我需要在用户单击元素或其中的任何内容时触发事件,除非他单击

我如何才能做到这一点?

您可以使用:

$("div").click(function (e) {
    if (e.target.nodeName.toLowerCase() != 'a') alert("hello world");
});

您可以使用:

$("div").click(function (e) {
    if (e.target.nodeName.toLowerCase() != 'a') alert("hello world");
});

因为您的
div
从来都不是
a
,所以这不起作用

您必须检查目标元素是否有如下标记:

$('div').click(function(event) {
    var $target = $(event.target);
    if(!$target.is("a") ) {
      alert("hello world");
    }
});

我已经更新了你的小提琴:

因为你的
div
从来都不是
a
,所以这不起作用

您必须检查目标元素是否有如下标记:

$('div').click(function(event) {
    var $target = $(event.target);
    if(!$target.is("a") ) {
      alert("hello world");
    }
});

我已经更新了你的小提琴:

这是未经测试的,但希望能给你一个想法:

$(function(){
    $('div').click(function(e){
         if($(e.target).is('a')){
              e.stopPropagation();
         }
    });
});

这是未经测试的,但希望能给你一个想法:

$(function(){
    $('div').click(function(e){
         if($(e.target).is('a')){
              e.stopPropagation();
         }
    });
});

可能与FYI重复,
div:not(a)
选择器没有多大意义。它的意思是“选择标记名为
div
且没有标记名
a
的每个元素”。由于一个元素不能有两个标记名,
div:not(a)
相当于
div
。可能重复的FYI,则
div:not(a)
选择器没有多大意义。它的意思是“选择标记名为
div
且没有标记名
a
的每个元素”。由于一个元素不能有两个标记名,
div:not(a)
相当于
div
。可能的重复项看起来非常熟悉:)您可能希望将“e”参数更新为“event”,以反映您在函数中使用的对象。谢谢,我没有注意到:-)这看起来非常熟悉:)您可能希望将“e”参数更新为“event”,以反映您在函数中使用的对象。谢谢,我没有注意到这一点。:-)