简体   繁体   English

如果单元格中的文本包含特定单词,则在其中添加图片#2

[英]If text in a cell contains a specific word then put an image in it #2

I want my table to convert specific text to images. 我希望我的桌子将特定的文本转换为图像。

I came close to a solution reading this thread: If text in a cell contains a specific word then put an image in it 我接近读取此线程的解决方案: 如果单元格中的文本包含特定单词,则在其中放入图片

The problem i experience is that the table only change the first text, if example "RED" occur a 2nd time it return blank to the cell instead of another red image. 我遇到的问题是表格仅更改了第一个文本,如果示例“ RED”第二次出现,它将返回空白到单元格而不是另一个红色图像。

I am very novice to programming and tried copy the code from the above thread, kind of know its wrong to do the repeat like I do instead of declaring it all in same paragraph but this is how it looks now and semi-works :) 我是编程的新手,并尝试从上述线程复制代码,有点像执行重复操作那样错误,而不是在同一段落中声明所有内容,但这是现在的样子,它是半成品:)

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

Really appreciate if someone could help me so each new cell that also contain the specific text get an image! 非常感谢有人能帮助我,让每个也包含特定文本的新单元格获得一张图片!

You are appending the same DOM element so you'll only end up with a single image. 您将附加相同的DOM元素,因此最终只会得到一个图像。

Move the image element creation inside your loop 将图像元素创建移动到循环中

for (var i = 0;i < allTableCells.length; i++) {
    var node = allTableCells[i];
    if (node.textContent.trim() === "RED") {
        node.textContent = "";
        var yourImage = new Image();
        yourImage.src = "red.png";
        node.appendChild(yourImage);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM