繁体   English   中英

如何使用Greasemonkey按ID号突出显示?

[英]How can I highlight, by id number, using Greasemonkey?

我想用Greasemonkey选择一些文本(突出显示为黄色)。 我该如何按ID号进行操作?

目标页面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";


您可以在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");
}

通过其ID获取元素,然后重新格式化应该可以工作

document.getElementById('l2289').style.color = '#FFFF00';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM