我有一个这样的脚本:
var target = $('#box1, #box2, #box3, #boxn').find('.content p');
target.hover(function() {
alert([the base element]);
})
我需要获取基本元素 #box1
或 #box2
或 #box3
等,其中包含鼠标悬停在其自身上的元素.
有办法做到这一点吗?
最佳答案
jQuery 事件处理程序有一个 general form可以处理你所问的问题:
// `.hover()` is a helper for handling both `mouseenter` and `mouseleave`
$('#box1, #box2, #box3, #boxn').on('mouseenter mouseleave', '.content p', function(event) {
// element that is currently the focus of the bubbled event;
// usually the same as `this`, the element on which the event was triggered
console.log(event.currentTarget);
// element to which this handler is bound
console.log(event.delegateTarget);
});
如果这样设置,.delegateTarget
将是您想要的父元素。
关于javascript - jquery中如何获取基本选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59346774/