使用 Greasemonkey 更改 Class 值?
Change Class value using Greasemonkey?
使用 Greasemonkey 更改 class 值的方法是什么?例如,我想更改:
<a id="Test" class="button_primary button_left_padding tlignore disabled" role="button"
至:
<a id="Test" class="button_primary button_left_padding tlignore enabled" role="button"
对于一个简单的静态页面,假设 id="Test"
是唯一且稳定的,则可以使用如下代码:
var targNode = document.getElementById ("Test");
targNode.classList.remove ("disabled");
targNode.classList.add ("enabled");
对于 AJAX 驱动的页面,这个完整的脚本可以工作:
// ==UserScript==
// @name _Flip CSS classes
// @match http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
//- The @grant directive is needed to restore the proper sandbox.
waitForKeyElements ("#Test", swapClass);
function swapClass (jNode) {
jNode.removeClass ("disabled").addClass ("enabled");
}
使用 Greasemonkey 更改 class 值的方法是什么?例如,我想更改:
<a id="Test" class="button_primary button_left_padding tlignore disabled" role="button"
至:
<a id="Test" class="button_primary button_left_padding tlignore enabled" role="button"
对于一个简单的静态页面,假设 id="Test"
是唯一且稳定的,则可以使用如下代码:
var targNode = document.getElementById ("Test");
targNode.classList.remove ("disabled");
targNode.classList.add ("enabled");
对于 AJAX 驱动的页面,这个完整的脚本可以工作:
// ==UserScript==
// @name _Flip CSS classes
// @match http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
//- The @grant directive is needed to restore the proper sandbox.
waitForKeyElements ("#Test", swapClass);
function swapClass (jNode) {
jNode.removeClass ("disabled").addClass ("enabled");
}