【发布时间】:2016-02-19 00:51:00
【问题描述】:
我有下一个 html:
<span data-typeId="123" data-type="topic" data-points="-1" data-important="true" id="the-span"></span>
是否可以获取以data- 开头的属性,并在 JavaScript 代码中使用它,如下面的代码?现在我得到null 作为结果。
document.getElementById("the-span").addEventListener("click", function(){
var json = JSON.stringify({
id: parseInt(this.typeId),
subject: this.datatype,
points: parseInt(this.points),
user: "H. Pauwelyn"
});
});
【问题讨论】:
-
现在(2019 年)还可以将节点的数据集属性与 SVG 节点(!)一起使用,请参阅 answer below 或使用 with D3。
标签: javascript html custom-data-attribute