Cutting HTML strings without breaking HTML tags

前端 未结 7 802
别跟我提以往
别跟我提以往 2021-01-04 19:34

How to write a function that can cut a string with HTML tags to an N-length string without breaking HTML tags while doing it.

The returned string doesn\'t need to b

7条回答
  •  孤街浪徒
    2021-01-04 19:39

    In javascript, you can use the textContent property of DOM elements to obtain this.

    HTML

    Hey Visit Croatia today

    Javascript

    var el = document.getElementById("mytext");
    console.log( el.textContent );
    //alert( el.textContent ); // if you don't have firebug.
    

提交回复
热议问题