add HTML to text node extracted via node.nodeValue

前端 未结 2 616
我寻月下人不归
我寻月下人不归 2020-12-06 13:43

I wanted to know if there\'s any way I could output HTML after extracting contents() and performing a replace on all of the text-nodes in it.

jsFiddle:

2条回答
  •  没有蜡笔的小新
    2020-12-06 14:29

    If I'm understanding you correctly, I believe this will do:

    $('#msg').contents().each(function() {
        if(this.nodeType == 3) {
            var u = this.nodeValue;
            var reg = /_link_/g;
            $(this).replaceWith(u.replace(reg,'Google'));
        }
    });
    

    http://jsfiddle.net/t8835/2/

提交回复
热议问题