我有一个 jQuery 事件,它是主干类的一部分,我试图从称为“数据前景”的数据属性中获取数据属性
我的函数如下所示..
foreground: function(e) {
if (e.target.id === "") {
this.findClickedElement(e, "li");
return false;
}
console.log(e.target.id); // this returns the id which is '115'
... more code
}
如何获取 li 上的数据属性,如下所示:
<li class="item" id="115" data-foreground="blue" />
最佳答案
例如在 vanillaJS 中使用 getAttribute()
e.target.getAttribute('data-foreground');
或使用数据集
,(不带data-
前缀访问)( MDN )
e.target.dataset.foreground
关于javascript - 从事件中获取数据属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30807126/