如何更改图片的背景颜色?它包含一个特定的链接

How do I change the background color of the <tr> that contains a certain link?

本文关键字:包含一 链接 颜色 何更改 背景      更新时间:2023-09-26

我想更改一个包含给定链接的"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>
  1. 看起来你在这里使用jQuery,它是如何加载的(由论坛或GM)
  2. 当jQuery被论坛加载时,你必须使用unsafeWindow.$
  3. 访问jQuery
  4. parent()返回一个jQuery-Object,你不能使用obj.style设置jQuery-Object的style-property,使用css()代替

我的建议是:(你不需要每一个)

unsafeWindow.$("tr:not(:has(tr)):has(a[href*='showforum=28'])").css('backgroundColor','red');

以jQuery的方式设置样式(如前所述,thisRow是一个jQuery对象)
那就是:thisRow.css ('backgroundColor', color1); .

然而,如果网站使用背景图像,颜色可能不会显示生效。用途:

thisRow.css ('background', color1);

,以确保任何BG图像不会掩盖颜色的变化。

最后,站点可以为表格单元格的BG设置样式,这同样可以掩盖行颜色的变化。为了解决这个问题,也可以为<td>元素设置样式。

也就是:

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