문제

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