简体   繁体   English

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

[英]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? 如果其中包含特定的单词,如何将图像放入td中? 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! PS对不起,我的英语! 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: 这是我们正在讨论的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>

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... 好的,看来您所有的代码都是正确的...页面中的某些内容导致功能异常...我无法在任何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 如果您有任何想法,请不要犹豫= D


Ok, something is missing. 好的,有些东西丢失了。 The code is working only if there is only one word in the td... My td's contain word 该代码仅在td中只有一个单词时才有效...我的td中包含单词
word
etc... 等等...

The script must find the good word in the td and show an image 脚本必须在td中找到合适的单词并显示图片

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

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

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