我有一个这样的脚本:
var target = $('#box1, #box2, #box3, #boxn').find('.content p');
target.hover(function() {
alert([the base element]);
})
我需要获取基本元素#box1
或#box2
或#box3
等,它包含将鼠标悬停在自身上的元素。
有没有办法做到这一点?
发布于 2019-12-15 22:09:26
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
将是您想要的父元素。
https://stackoverflow.com/questions/59346774
复制