jQuery与非选择器一起使用.on() [英] JQuery use .on() with not selector

查看:91
本文介绍了jQuery与非选择器一起使用.on()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.on()函数,其语法如下:

I am using .on() function with this syntax:

$(".schemaTab").on("click", ".schema", function () {})

而不是.live(),因为我正在动态添加.schema div.这些div具有以下代码:

instead of .live() because I am adding .schema divs dynamically. Those divs have this code:

<div class="schema" id="SCH_16">
   <img src="Img/btnRemove.png" class="ikona SCH_Remove" alt="Smazat" title="Smazat">
</div>

但是我需要将img从其父div的click事件中排除,所以知道如何做到这一点吗?

But I need to exclude the img from click event of its parent div, so any idea how to do it?

我尝试过:

$(".schemaTab").not(".SCH_Remove").on("click", ".schema", function () {})

...但是它不起作用,我也不知道如何在.on()代码内添加.not()

...but it didn't work and I don't know how to add the .not() inside the .on() code

推荐答案

.not()不起作用,因为on方法将事件处理程序附加到jQuery对象中当前选定的元素集,并使用原始选择器测试该事件是否适用.

.not() won't work because on method attaches event handlers to the currently selected set of elements in the jQuery object and uses the original selector to test whether the event should apply.

所以我认为选择并在您的情况下进行过滤将达到目的.

So I reckon selecting and then filtering in your case will do the trick.

API: http://api.jquery.com/on/

您可以尝试以下方法:

$(".schemaTab").on("click", ".schema", function (e) {

    if (!$(e.target).is(".schemaTab")) {
         // not stuff in here
    }

});

这篇关于jQuery与非选择器一起使用.on()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆