javascript - 我可以使用它来引用委托(delegate)元素的选择器吗?

标签 javascript jquery

例如:

$('#enquiry-form').on('click','.close', function(){ 
    $(this)..code..
});

$(this) 会引用 '.close' 但有没有办法引用 $('#enquiry-form')以类似于使用 javascript 的 this$(this)?

的方式

最佳答案

您可以使用 event.delegateTarget获取委托(delegate)目标。

Live Demo

$('#enquiry-form').on('click','.close', function(event){ 
     alert($(event.delegateTarget)[0].id);
});

This property is most often useful in delegated events attached by .delegate() or .on(), where the event handler is attached at an ancestor of the element being processed. It can be used, for example, to identify and remove event handlers at the delegation point, jQuery Doc.

关于javascript - 我可以使用它来引用委托(delegate)元素的选择器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22065996/

相关文章:

jquery - 为什么我的 jquery 展开/折叠行不起作用?

javascript - Phonegap 中的基本图形元素

javascript - 在单个页面上使用多个(猫头鹰)轮播

javascript - 如何重命名 React 组件_并且_也重构了 'imports'?

javascript - 将独立的 View 文件放入codeigniter中另一个html中的div中

javascript - 仅限 Android Chrome : Changing input val during keyup doesn't apply

javascript - 将父级的子级 ul 从左向右滑动 jquery

Javascript:基于变量自定义数组推送 - 允许某些类型的标准推送调用

javascript - document.execCommand ('copy' ) 在 React 中不起作用

javascript - 加载并单击时隐藏/显示 div