li悬停怎么获得它的下标
如果是在HTML页面上,可以使用JavaScript来获取li元素的下标。可以通过以下代码实现:
var lis = document.getElementsByTagName("li");
for (var i = 0; i < lis.length; i++) {
lis[i].addEventListener("mouseover", function() {
var index = Array.prototype.indexOf.call(this.parentElement.children, this);
console.log(index);
});
}
以上代码中,我们使用addEventListener()方法来给每个li元素添加鼠标悬停事件,当鼠标悬停在某个li元素上时,会输出该元素在其父元素中的下标。具体实现是通过使用Array.prototype.indexOf()方法和call()方法来获取元素在其父元素的子元素中的位置。
原文地址: https://www.cveoy.top/t/topic/uPW 著作权归作者所有。请勿转载和采集!