开发者

jQuery contents() causing problems in IE

I'm using the contents() function in jQuery in order to obtain some text within a LI which I can't target directly with a span or other selector.

The code I've written is as follows:

$('#tierArray_' + tierId).contents().each(function(i, node) {
  if (node.nodeName == "#text") {
    node.textC开发者_StackOverflow社区ontent = name;
  }
});

This works fine in Firefox and changes the target text to what is set in 'name'. In IE however I get the following errors:

"Object doesn't support this property or method"

This seems to relate to the line "node.textContent = name" as when I comment this out the error disappears.

In a nutshell I'm simply trying to replace some text with newly created text, the HTML markup is as follows:

<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>

So the text after the last span (Bla 4aaa11) will need to be replaced with some newly generated text.


Internet Explorer does not support the textContent property.

However, since you only want to replace the contents of a text node, you should be able to use nodeValue instead:

$("#tierArray_" + tierId).contents().each(function(i, node) {
    if (node.nodeName == "#text") {
        node.nodeValue = name;
    }
});


the .textContent isn't supported by IE. You may have luck setting the .innerText in IE instead:

if(typeof(node.textContent) != 'undefined'){
  node.textContent = name;
} else {
  node.innerText = name;
}


Use data or nodeValue instead:

node.data = name;
node.nodeValue = name;

Both are widely supported across browsers:

Quirksmode: nodeValue

Quirksmode: data

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜