Javascript 如何更改<;的背景色;tr>;包含某个链接的?

Javascript 如何更改<;的背景色;tr>;包含某个链接的?,javascript,greasemonkey,background-color,tablerow,Javascript,Greasemonkey,Background Color,Tablerow,我想更改包含给定链接的“tr”元素的背景色 我的片段: var color1 = "red"; var targetForum = $("tr a[href*='showforum=28']"); targetForum.each ( function () { var thisRow = $(this).parent ().parent ().parent (); thisRow.style.backgroundColor = color1; } 我尝试了r

我想更改包含给定链接的“tr”元素的背景色

我的片段:

var color1 = "red";
var targetForum = $("tr a[href*='showforum=28']");

targetForum.each ( function () {
    var thisRow = $(this).parent ().parent ().parent ();        
    thisRow.style.backgroundColor = color1;
}
我尝试了rgb值和十六进制,但bg颜色没有任何变化

目标页面如下所示:

<tr>
    <td class="row2" align="center">
    <td class="row1" width="3%" align="center">
    <td class="row1">
    <td class="row1" width="15%">
        <span class="forumdesc">
            <a title="Off-Topic" href="showforum=41">ForumABC</a>
        </span>
    </td>
    <td class="row1" align="center">
    <td class="row2" align="center">
    <td class="row1" align="center">223460</td>
    <td class="row1">
</tr>

223460
  • 看起来您正在使用jQuery,它是如何加载的(由论坛还是由GM加载的)
  • 当论坛加载jQuery时,您必须使用
    unsafeWindow访问jQuery。$
  • parent()返回jQuery对象,不能使用
    obj.style
    设置jQuery对象的样式属性,请改用
  • 我建议:(你不需要每个人)

  • 看起来您正在使用jQuery,它是如何加载的(由论坛还是由GM加载的)
  • 当论坛加载jQuery时,您必须使用
    unsafeWindow访问jQuery。$
  • parent()返回jQuery对象,不能使用
    obj.style
    设置jQuery对象的样式属性,请改用
  • 我建议:(你不需要每个人)


    以jQuery的方式设置样式(正如前面指出的,
    thisRow
    是一个jQuery对象)。
    那就是:
    thisRow.css('backgroundColor',color1)

    但是如果站点使用背景图像,则颜色可能不会生效。使用:

    thisRow.css ('background', color1);
    
    以确保任何BG图像不会掩盖颜色变化

    最后,站点可能会设置表格单元格的背景样式,这可能再次掩盖行颜色的变化。要解决这个问题,还可以为
    元素设置样式

    这就是:

    targetForum.each ( function () {
        var thisRow = $(this).parent ().parent ().parent ();        
        thisRow.css ('background', color1);
        thisRow.find ("td").css ('background', color1);
    }
    

    以jQuery的方式设置样式(正如前面指出的,
    thisRow
    是一个jQuery对象)。
    那就是:
    thisRow.css('backgroundColor',color1)

    但是如果站点使用背景图像,则颜色可能不会生效。使用:

    thisRow.css ('background', color1);
    
    以确保任何BG图像不会掩盖颜色变化

    最后,站点可能会设置表格单元格的背景样式,这可能再次掩盖行颜色的变化。要解决这个问题,还可以为
    元素设置样式

    这就是:

    targetForum.each ( function () {
        var thisRow = $(this).parent ().parent ().parent ();        
        thisRow.css ('background', color1);
        thisRow.find ("td").css ('background', color1);
    }
    

    你安装了Firebug吗?检测此类错误非常有用。您是否安装了Firebug?检测这种错误是非常有用的。我知道我遗漏了一些东西,但是为什么
    取消安全提示。$
    ?现在,我开始后悔最后一杯酒=/请阅读以下内容:。如果要访问窗口/文档中的用户定义对象(例如jQuery),则需要使用unsafeWindow指向窗口对象。本机JS/DOM对象不受此限制的影响,但是当GM()加载jQuery时,您不需要使用unsafeWindow,最好不要建议使用
    unsafeWindow
    ——除了少数需要使用目标页面JS的实例(这不是其中之一)。它不仅“不安全”,还会导致范围/代码混淆,并引入不必要的依赖项。我知道我遗漏了一些东西,但为什么
    unsafewWindow.$
    ?现在,我开始后悔最后一杯酒=/请阅读以下内容:。如果要访问窗口/文档中的用户定义对象(例如jQuery),则需要使用unsafeWindow指向窗口对象。本机JS/DOM对象不受此限制的影响,但是当GM()加载jQuery时,您不需要使用unsafeWindow,最好不要建议使用
    unsafeWindow
    ——除了少数需要使用目标页面JS的实例(这不是其中之一)。它不仅“不安全”,而且会导致范围/代码混淆,并引入不必要的依赖关系。