Replace a textNode with HTML text in Javascript?

后端 未结 2 1904
抹茶落季
抹茶落季 2021-01-07 18:37

I was directed to the Linkify project on GitHub (https://github.com/cowboy/javascript-linkify) for finding and \"linkifying\" URLs and domains just floating in text.

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-07 18:59

    Additionally to previous answer I propose more short way (based on jQuery):

    $(n).replaceWith('Some text with html support');
    

    where n - is textNode.

    Or the native version

    var txt = document.createElement("span");
    txt.innerHTML = "Some text with html support";
    node.replaceWith(txt);
    

    where node is the textNode

提交回复
热议问题