我有以下 jQuery 代码:
$('#temp_modals').on('mouseleave','*[data-username]',function() {
///my logic
});
但是我需要在 *[data-username]
的父 div 上有 mouseleave 事件。
由于这个元素是动态添加到网页的,所以我不能使用普通的选择器,需要使用 $.on。
简而言之,我想实现这样的代码运行。
$('#temp_modals').on('mouseleave','*[data-username]'.parent(),function() {
///my logic
});
我该怎么做?
最佳答案
尝试
$('#temp_modals').on('mouseleave','*:has(> [data-username])',function(event) {
///my logic
// this should get you the parent.
$(event.target).parent();
});
演示:Fiddle
关于javascript - 如何在选择器上的 jquery 中访问父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18351611/