jQuery contents()导致IE出现问题
我在jQuery中使用contents()函数来获取LI中的一些文本,而这些文本不能直接用span或其他选择器作为目标 我编写的代码如下:jQuery contents()导致IE出现问题,jquery,text,Jquery,Text,我在jQuery中使用contents()函数来获取LI中的一些文本,而这些文本不能直接用span或其他选择器作为目标 我编写的代码如下: $('#tierArray_' + tierId).contents().each(function(i, node) { if (node.nodeName == "#text") { node.textContent = name; } }); <li class="ui-state-default" id="tierArray_
$('#tierArray_' + tierId).contents().each(function(i, node) {
if (node.nodeName == "#text") {
node.textContent = name;
}
});
<li class="ui-state-default" id="tierArray_105">
<span style="display: block;" id="revoke_105">
<a class="tierStatus" title="Revoke" href="#">Revoke</a> |
<a class="tierEdit" id="edit_tier_105" title="Bla 3aaa11" href="#">Edit</a>
</span>
<span style="display: none;" id="active_105">
<a class="tierStatus" title="Activate" href="#">Activate</a> |
<a class="tierEdit" id="edit_tier_105" title="Bla 3aaa11" href="#">Edit</a>
</span>
Bla 3aaa11
</li>
这在Firefox中运行良好,并将目标文本更改为“name”中设置的文本。然而,在IE中,我得到以下错误:
“对象不支持此属性或方法”
这似乎与行“node.textContent=name”有关,因为当我注释掉这一行时,错误消失了
简而言之,我只是想用新创建的文本替换一些文本,HTML标记如下:
$('#tierArray_' + tierId).contents().each(function(i, node) {
if (node.nodeName == "#text") {
node.textContent = name;
}
});
<li class="ui-state-default" id="tierArray_105">
<span style="display: block;" id="revoke_105">
<a class="tierStatus" title="Revoke" href="#">Revoke</a> |
<a class="tierEdit" id="edit_tier_105" title="Bla 3aaa11" href="#">Edit</a>
</span>
<span style="display: none;" id="active_105">
<a class="tierStatus" title="Activate" href="#">Activate</a> |
<a class="tierEdit" id="edit_tier_105" title="Bla 3aaa11" href="#">Edit</a>
</span>
Bla 3aaa11
</li>
|
|
Bla 3AA11
因此,最后一个span(Bla 4aaa11)之后的文本需要替换为一些新生成的文本。IE不支持
.textContent
。您可能会幸运地在IE中设置.innerText
:
if(typeof(node.textContent) != 'undefined'){
node.textContent = name;
} else {
node.innerText = name;
}
IE不支持
.textContent
。您可能会幸运地在IE中设置.innerText
:
if(typeof(node.textContent) != 'undefined'){
node.textContent = name;
} else {
node.innerText = name;
}
改用
数据
或节点值
:
node.data = name;
node.nodeValue = name;
$("#tierArray_" + tierId).contents().each(function(i, node) {
if (node.nodeName == "#text") {
node.nodeValue = name;
}
});
两种浏览器都广泛支持:
使用
数据
或节点值
:
node.data = name;
node.nodeValue = name;
$("#tierArray_" + tierId).contents().each(function(i, node) {
if (node.nodeName == "#text") {
node.nodeValue = name;
}
});
两种浏览器都广泛支持:
Internet Explorer不支持该属性 但是,由于您只希望替换文本节点的内容,因此应该可以使用:
Internet Explorer不支持该属性 但是,由于您只希望替换文本节点的内容,因此应该可以使用: