我正在尝试根据文本在特定表格的单元格中添加锚点。这是我到目前为止所得到的......
for (var d = 0, max = allTableCells.length; d < max; d++) {
var node = allTableCells[d];
var currentText = node.childNodes[0].nodeValue;
if (currentText === "@WebSecurity.CurrentUserName + 'TEXT'")
node.childNodes[0].nodeValue = '<a name="CP"></a>'
}
}
这里的主要问题似乎是那两行...
if (currentText === "@WebSecurity.CurrentUserName + 'TEXT'")
node.childNodes[0].nodeValue = '<a name="CP"></a>'
如果有人能帮助我,那就太好了......谢谢
以下是该页面的完整脚本: 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"
}
for (var c = 0, max = allTableCells.length; c < max; c++) {
var node = allTableCells[c];
var currentText = node.childNodes[0].nodeValue;
if (currentText === "None")
node.style.color = "black";
node.style.border = "solid"
}
var d, max, node, nodes, currentText, anchor;
for (d = 0, max = allTableCells.length; d < max; d++) {
node = allTableCells[d];
nodes = node.childNodes;
currentText = nodes[nodes.length - 1].nodeValue;
if (@WebSecurity.CurrentUserName === nodes[0].nodeValue && currentText === 'CP') {
anchor = document.createElement('a');
anchor.name = 'CP';
allTableCells[d].insertBefore(anchor, nodes[0]);
}
}
</script>
答案 0 :(得分:0)
像<td>Bob<br>text random<br>CP</td>
这样的单元格包含下面的childNodes
:
nodes = td.childNodes;
nodes[0] === Bob
nodes[1] === br element
nodes[2] === text random
nodes[3] === br element
nodes[4] === CP
请注意,如果</td>
与CP
不在同一行,则还会nodes[5] === empty text node
。
如果要检查的文本(评论中的CP
)始终位于最后一个节点中,则需要更改以下代码:
var d, max, node, nodes, currentText, currentUser, anchor;
for (d = 0, max = allTableCells.length; d < max; d++) {
node = allTableCells[d];
nodes = node.childNodes;
currentUser = parent.@WebSecurity.CurrentUserName;
currentText = nodes[nodes.length - 1].nodeValue;
if (currentUser === nodes[0].nodeValue && currentText === 'CP') {
anchor = document.createElement('a');
anchor.name = 'CP';
allTableCells[d].insertBefore(anchor, nodes[0]);
}
}
修改强>
在评论中,您已告知此代码在iframe中执行。我假设在主窗口中定义了@WebSecurity.CurrentUserName
。确保它是主窗口中的全局变量,然后您可以使用上面编辑的代码段。