更改文本节点文本 [英] Change text-nodes text

查看:137
本文介绍了更改文本节点文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改文本节点文本?

How can I change text-nodes text?

<p class='theClass'> bbb <a href=#> foo</a> aaa </p>

我正在尝试将aaa和bbb更改为 hello世界。我成功地选择了这些节点,但是无法更改其文本。

I'm trying to change 'aaa' and 'bbb' to hello world. I successed to select those nodes but couldn't change their text.

var $textNodes = $('.theClass').contents().filter(function() {
    return this.nodeType == Node.TEXT_NODE;
});

JSFiddle

我可以用这个 $ textNodes 如何更改文本? / p>

What can I do with this $textNodes to change their text?

推荐答案

使用 nodeValue code>属性的文本节点。两者同样有效并得到很好的支持:

Use the nodeValue or data property of the text node. Both are equally valid and well supported:

$textNodes.each(function() {
    this.data = "CHANGED";
});

顺便提一句, Node.TEXT_NODE 不存在在IE< 9,所以你最好不要使用 3

Incidentally, Node.TEXT_NODE does not exist in IE < 9, so you'd be better off simply using 3 instead.

这篇关于更改文本节点文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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