有一个包含此代码的网站
<div id = "agreement_btn" class = "agreement_form button" gt-form-status = "">
我想调整标签的值((gt-form-status)) 这样最后:
<div id = "agreement_btn" class = "agreement_form button" gt-form-status = "signed">
通过创建以javascript开头的书签:....
我可以这样做吗? 问候
答案 0 :(得分:1)
您可以使用Element.setAttribute
在元素上设置不存在的属性。
document.querySelector('#agreement_btn').setAttribute('gt-form-status', 'signed');
var element = document.querySelector('#agreement_btn');
console.log('element before setAttribute:', element.outerHTML);
element.setAttribute('gt-form-status', 'signed');
console.log('element after setAttribute:', element.outerHTML);
<div id = "agreement_btn" class = "agreement_form button" gt-form-status = ""></div>
但是,最好使用数据属性,以防以后使用的某些属性标准化。
var element = document.querySelector('#agreement_btn');
console.log('element before set attribute:', element.outerHTML);
element.dataset.gtFormStatus = "signed";
console.log('element after set attribute:', element.outerHTML);
<div id = "agreement_btn" class = "agreement_form button" data-gt-form-status = ""></div>