我想用 Greasemonkey 选择一些文本(突出显示为黄色)。我怎样才能通过身份证号码做到这一点?
目标页面 HTML:
<div class="answer" style="margin-top: 100px;">
<li id="l2289" onclick="sec(2289)" class="answer-sk">italya</li>
<li id="l2290" onclick="sec(2290)" class="answer-sk">Fransa</li>
<li id="l2291" onclick="sec(2291)" class="answer-sk">ingiltere</li>
<li id="l2292" onclick="sec(2292)" class="answer-sk">Portekiz</li>
</div>
最佳答案
令人惊讶的是,这个问题以前似乎没有被问过——至少在 Greasemonkey 脚本的上下文中是这样。
要在静态页面上按 ID 突出显示,只需设置 CSS 背景即可。这是一个完整的脚本:
// ==UserScript==
// @name _Highlite node with id l2290
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
//-- Highlite the node with ID: l2290
var targNode = document.getElementById ("l2290");
targNode.style.background = "yellow";
您可以see underlying code in action at jsFiddle 。
该代码适用于所有现代浏览器、用户脚本引擎。
要在动态(AJAX 驱动)页面上按 ID 突出显示,需要轮询或突变观察者,或 AJAX 拦截。
以下脚本适用于大多数引擎。在 Chrome 上,请务必使用 Tampermonkey 安装脚本:
// ==UserScript==
// @name _Highlite node with id l2290
// @include 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 work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
waitForKeyElements ("#l2290", highlightNode);
function highlightNode (jNode) {
jNode.css ("background", "yellow");
}
关于javascript - 如何使用 Greasemonkey 按 ID 号突出显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25054148/