Strip HTML from Text JavaScript

前端 未结 30 3570
北荒
北荒 2020-11-21 05:08

Is there an easy way to take a string of html in JavaScript and strip out the html?

30条回答
  •  情歌与酒
    2020-11-21 05:24

    The accepted answer works fine mostly, however in IE if the html string is null you get the "null" (instead of ''). Fixed:

    function strip(html)
    {
       if (html == null) return "";
       var tmp = document.createElement("DIV");
       tmp.innerHTML = html;
       return tmp.textContent || tmp.innerText || "";
    }
    

提交回复
热议问题