0

我希望有一个人可以帮助我。如果图像中有特定单词,我如何将图像放入 td 中?这是我的代码:

    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对不起我的英语!谢谢!

这是现在的完整代码......仍然无法正常工作=/

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);
     }

好的,最后保存的代码是这个......仍然不起作用:(对不起我的英语和缺乏知识)

<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>

这是我们正在谈论的 td 的示例:

       <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>

好的,看起来您的所有代码都是正确的...我页面中的某些内容导致了功能障碍...我无法将图像放在任何 td 的后面...

我正在尝试逐步重写我的页面,看看它在哪里停止。如果您有任何想法,请不要犹豫 =D


好吧,有些东西不见了。仅当 td 中只有一个单词时,该代码才有效……我的 td 包含单词
word
等……

脚本必须在td中找到好词并显示图片

4

1 回答 1

0
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);
    }
}
于 2013-08-31T00:04:07.833 回答