Using the following,

var cells = document.getElementById("test").getElementsByTagName("td");
for (var i = 0; i < cells.length; i++) {
    if (cells[i].innerHTML == "one") {
        cells[i].style.backgroundColor = "red";
    }
}

http://jsfiddle.net/jfriend00/Uubqg/

does anyone know how I can locate any word in a row and have it highlight a specific cell in the same row

take for instance if the word one is found anywhere, it highlights the first cell in that row?

有帮助吗?

解决方案

To highlight the first cell, just go back to the parent rows and get the cells:

var cells = document.getElementById("test").getElementsByTagName("td");
for (var i = 0; i < cells.length; i++) {
    if (cells[i].innerHTML == "one") {
        var row = cells[i].parentNode;
        row.getElementsByTagName("td")[0].style.backgroundColor = "red";
    }
}

其他提示

sure, just look at the cell's parentNode, and then children[0] like this:

var cells = document.getElementById("test").getElementsByTagName("td");
for (var i = 0; i < cells.length; i++) {
    if (cells[i].innerHTML == "one") {
        cells[i].parentNode.firstChild.children[0].style.backgroundColor = "red";
    }
}

working fiddle:

http://jsfiddle.net/Uubqg/47/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top