如何在任何网站上更改标签值
How to change tag value on any web site
有一个网站包含此代码
<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 开头的小书签:....
我可以这样做吗?
问候
您可以使用 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>
有一个网站包含此代码
<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 开头的小书签:....
我可以这样做吗? 问候
您可以使用 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>