如果单元格中的文本包含特定的单词,则在其中放置一个图像 [英] If text in a cell contains a specific word then put an image in it

查看:102
本文介绍了如果单元格中的文本包含特定的单词,则在其中放置一个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能帮助我。如果有一个特定的单词,我怎么能把一个图像放在td中?这里是我的代码:

  var allTableCells = document.getElementsByTagName(td); (var i = 0,max = allTableCells.length; 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; (var i = 0,max = allTableCells.length; 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 var node = allTableCells [j];
if(Text.toString()。trim()===ArmHW){
node.removeChild(Text);
node.appendChild(yourHW);
}

确定上次保存的代码是这一个...仍然不起作用:
(对不起,我的英语和知识缺乏)

 < script> 
var allTableCells = document.getElementsByTagName(td); (var i = 0,max = allTableCells.length; 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); (var i = 0,max = allTableCells.length; i var node = allTableCells [i];




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


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

以下是我们正在讨论的td的示例:

 < table id =91class =csmap> @foreach(db.Query中的var行(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包含单词
单词等...



脚本必须在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);
}
}


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')"

P.S. 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);
    }
}

这篇关于如果单元格中的文本包含特定的单词,则在其中放置一个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆