Jquery: Strip all specific HTML tags from string

后端 未结 4 2090
攒了一身酷
攒了一身酷 2020-12-31 20:52

I have a variable that contains a string of text and html tags, such as:

var temp = \"
Some text

More texthere

4条回答
  •  天涯浪人
    2020-12-31 21:31

    Demo: http://jsfiddle.net/VwTHF/1/

    $('span, p').contents().unwrap();
    

    .contents() will get the elements and text within each such tag, and .unwrap will remove the element wrapping each content section.

    Based on your current approach it would look something like this:

    var temp = "
    Some text

    More texthere

    Even more

    "; var $temp = $(temp); $temp.find('span, p').contents().unwrap().end().end();

    If you want to continue targeting the original object, you have to use .end() to clear the filter.

提交回复
热议问题