javascript - jquery中如何获取基本选择器

标签 javascript jquery

我有一个这样的脚本:

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/

相关文章:

javascript - 超过一定宽度时重新加载页面

javascript - 在 Bootstrap 导航栏中分离菜单项并将它们居中

jQuery 插件(如果对象存在)

JavaScript:检查变量是否等于两个或多个值之一的简单方法?

javascript - Jquery 模态/对话框表单不起作用

javascript - 同时弹出并更改位置

javascript - 要求使用 AMD 模式为 jQuery UI 事件提供错误

javascript - 在 jQuery 中替换选定的 HTML 文本

php - 使用 ajax 时传递给 implode 的参数无效

javascript - 如何更改 pdf.js 显示的文本?