返回 undefined 的 closest() 元素的 ClassName

ClassName of of closest() element returning undefined

提问人:Asad Zubair Bhatti 提问时间:2/4/2021 更新时间:2/4/2021 访问量:160

问:

我正在尝试检索我正在使用 target.closest() 方法检索的元素的类名(并因此使用它)。但是,当我尝试应用classNames甚至使用classList方法做某事时,结果是不确定的。

我在stackoverflow上看到了一个类似的问题,但是这个人正在使用html集合,通过使用单个元素,他的问题得到了解决。但是,我的问题仍然是一样的。

    const cuisineList = document.querySelector('.cuisinesList');

    cuisineList.addEventListener('mouseover', function (e) {
    const hovered = e.target.closest('li');
    console.log("hello", hovered.classNames);
})

旁注:正确接收给定代码中的“悬停”元素。这没有问题。悬停元素的结果就是我想要操作的列表元素。

javascript 最接近 事件委托 dom-traversal

评论


答: 暂无答案