简体   繁体   中英

Use Greasemonkey to change html table cell format based on its content

I'm way out of my depth with a greasemonkey script. I'm dealing with an html table where the second column has month data in, eg 'April', or 'May'. Here's a simplified version of the html I'm dealing with:

<html>
    <body>
        <form>
            <table class="gridtable">
                <tbody>
                    <tr class="header"></tr>
                    <tr>
                        <td>blah</td>
                        <td>April</td>
                        <td>blah</td>
                    </tr>
                    <tr>
                        <td>blah</td>
                        <td>May</td>
                        <td>blah</td>
                    </tr>
                </tbody>
            </table>
        </form>
    </body>
</html>

For all rows where the 2nd column/TD contains 'May', I want to use greasemonkey to change the formatting of that cell, eg to bold red text, with a yellow background. Here's the code I have so far, but it's not having any effect, and I'm not sure if it's a good starting point (I've only included background color for now, walk before run):

var thetds = document.getElementsByTagName('td');
for (var j = 0; j < thetds.length; j++) {
if (thetds[j].innerHTML == "May") 
    thetds[j].style.backgroundColor = rgb(250, 220, 0);
}

Effectively I want the td to turn from:

<td>May</td>

to:

<td style="background-color: rgb(250, 220, 0); color: rgb(255, 0, 0); font-weight: bold;">May</td>

Any advice greatly appreciated! Thank you. PS I did find this similar question but I can't flex it to my situation, it is quite different.

rgb(250, 220, 0) should be "rgb(250, 220, 0)"

thetds[j].style.backgroundColor = "rgb(250, 220, 0)";

example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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