我希望有人可以帮助我。如果有一个特定的单词,我如何将图像放入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')"
P.S。对不起我的英语不好!谢谢你!
以下是完整的代码......仍然无效= /
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包含单词
单词等...
脚本必须在td中找到好词并显示图像
答案 0 :(得分: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);
}
}