小编Abh*_*dey的帖子

将HTML添加到通过node.nodeValue提取的文本节点

我想知道在提取contents()并在其中的所有文本节点上执行替换后,是否有任何方法可以输出HTML .

jsFiddle:http://jsfiddle.net/bikerabhinav/t8835/1/

HTML:

<div id="msg">
    Hi, this is a Textnode _link_<br>
    this is Textnode number 2
    <br /><a href="http://google.com">element node</a>
</div>
Run Code Online (Sandbox Code Playgroud)

JS:

$('#msg').contents().each(function() {
    if(this.nodeType == 3) {
        var u = this.nodeValue;
        var reg = /_link_/g;
        this.nodeValue = u.replace(reg,'<a href="http://google.com">Google</a>');
    }
});
Run Code Online (Sandbox Code Playgroud)

OUTPUT:

Hi, this is a Textnode <a href="http://google.com">Google</a>
this is Textnode number 2 
element node
Run Code Online (Sandbox Code Playgroud)

我需要的:

Hi, this is a Textnode <a href="http://google.com">Google</a><br> <!-- as a HTML link -->
this is Textnode …
Run Code Online (Sandbox Code Playgroud)

javascript jquery

4
推荐指数
1
解决办法
1910
查看次数

标签 统计

javascript ×1

jquery ×1