javascript - 如何更改自己定义的属性

标签 javascript jquery html

我想知道如何使用 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/

相关文章:

html - 根据屏幕大小动态换行文本?

javascript - ReactJS 不显示 backgroundImage 内联 CSS

javascript - JQuery:切换问题

html - 在div上叠加透明按钮

javascript - jQuery 图片 slider 问题

javascript - Google Plus One 按钮回调 - 'subscribe' 到 +1 操作的任何方式?

javascript - jQuery 在过滤 AJAX 响应时返回未定义

javascript - 恢复或设置 Brightcove 视频的开始时间

javascript - PrettyPhoto 画廊中的最后一张图片首先加载

jquery - 是否可以使用 jquery 附加一个 css 属性?