如何在任何网站上更改标签值

时间:2018-11-10 19:44:26

标签: javascript jquery html html5 bookmarklet

有一个包含此代码的网站

<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开头的书签:....

我可以这样做吗? 问候

1 个答案:

答案 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>