Jquery Selector use of not



我有一个类名为".modalPopup"的div,在div内部我有多个表,在表内部我有div,在文本框(input)和下拉列表(select)等多个控件内部。

我想要的是,当用户点击主div上除dropdownlist(select)外具有"modalPopup"的任何位置时,我想要触发点击事件。我尝试使用我的选项,但无法获取特定的单击事件。像这样:

  $('.modalPopup :not(modalPopup select)').click(function(){
    document.write("working for click except select ");
    });

谁能给我提供解决方案吗?

最简单的方法是将div上的点击事件与modalPopup类绑定,然后使用e.target:检查用户实际点击了什么

$('.modalPopup').click(function(e){
    if (!$(e.target).is('select')) {
        document.write("working for click except select ");
    }
});

e.target是单击的dom元素。$(e.target).is('select')获取该dom元素并从中创建一个jQuery对象,并检查它是否为select。

JSFiddle示例

在not.()条件中.modalPopup之前缺少一个点。

$('.modalPopup :not(.modalPopup select)').click(function(){ document.write("working for click except select "); });

相关内容

最新更新