我想知道如何使用 JavaScript/jQuery 更改 html 标签中自己定义的属性。 例如: 我定义了称为升序的属性:
<th ascending="true" id="1">some text</th>
在 JavaScript/jQuery 中我想更改“false”属性 我正在尝试这种方式,但它不起作用(我猜这个选项仅适用于预定义的属性):
var tag = document.getElementById("1");
tag.ascending = "false";
最佳答案
添加自定义时使用自定义data-*
属性,否则无法通过验证!对于您的情况:
<th data-ascending="true" id="1">some text</th>
获取/设置(纯 JS):
var tag = document.getElementById("1");
tag.getAttribute("data-ascending"); //get
tag.setAttribute("data-ascending", true); //set
jQuery:
$("#1").data("ascending"); //get
$("#1").data("ascending", true); //set
关于javascript - 如何更改自己定义的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19845899/