简体   繁体   中英

If text in a cell contains a specific word then put an image in it

I hope someone can help me. How can I a put an Image in a td if there is a specific word in it? Here is my code:

    var allTableCells = document.getElementsByTagName("td");

    for (var i = 0, max = allTableCells.length; i < max; i++) {
        var node = allTableCells[i];


        var currentText = node.childNodes[0].nodeValue;


        if (currentText === "ArmHW")
            node.style.backgroundImage = "url('HW.png')"

PS Sorry for my english! And thank you!

Here is the complete code now... still not working =/

var allTableCells = document.getElementsByTagName("td");
var HW = new Image();
yourHW.src = "HW.png";

for (var i = 0, max = allTableCells.length; i < max; i++) {
    var node = allTableCells[i];

    var currentText = node.childNodes[0].nodeValue;

    if (currentText === "@WebSecurity.CurrentUserName")
        node.style.color = "Blue"; 
        node.style.border = "solid"

}

for (var j = 0, max = allTableCells.length; j < max; i++) {
var node = allTableCells[j];
if (Text.toString().trim() === "ArmHW") {
    node.removeChild(Text);
    node.appendChild(yourHW);
     }

Ok the last saved code is this one... Still doesn't work: (Sorry for my english and lack of knowledge)

<script>
var allTableCells = document.getElementsByTagName("td");


for (var i = 0, max = allTableCells.length; i < max; i++) {
    var node = allTableCells[i];

    var currentText = node.childNodes[0].nodeValue;

    if (currentText === "@WebSecurity.CurrentUserName")
        node.style.color = "Blue"; 
        node.style.border = "solid";

}
var allTableCells = document.getElementsByTagName("td");

for (var i = 0, max = allTableCells.length; i < max; i++) {
    var node = allTableCells[i];


    var currentText = node.childNodes[0].nodeValue;


    if (currentText === "ArmHW")
        node.style.backgroundImage = "url('HW.png')"
}
</script>

And here is a sample of the td we are talking about:

       <table id="91" class="csmap">@foreach(var row in db.Query(selectCommand, Searcdb.Query(selectCommand, SearchTerm90)){<tr><td onmouseover="this.style.border='dashed'" onmouseout="this.style.border='solid'">@row.Owner<br>@row.Owner2</td></tr>}</Table>

Ok, it looks like all your code are right... Something in my page is causing disfunction... I can't put an image in the back of any td...

I'm trying to rewrite my page step by step and see where it stops. If you have any idea do not hesitate =D


Ok, something is missing. The code is working only if there is only one word in the td... My td's contain word
word
etc...

The script must find the good word in the td and show an image

var allTableCells = document.getElementsByTagName("td");

var yourImage = new Image();
yourImage.src = "HW.png";

for (var i = 0;i < allTableCells.length; i++) {
    var node = allTableCells[i];
    if (node.textContent.trim() === "ArmHW") {
        node.textContent = "";
        node.appendChild(yourImage);
    }
}

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