質問

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