How to strip out
tags and wrap lines with

and

tags?

前端 未结 1 1956
一生所求
一生所求 2021-01-16 08:17

This is what the HTML code looks like. I need to strip out the br tags and wrap the lines in paragraph tags (ie the lines with dates). Each date should be wrapped in its o

相关标签:
1条回答
  • 2021-01-16 08:34

    Edit: By using <p> tags in that manner your runining it's whole purpose instead you should try removing the <br> tags and placing the text inside <p> elements, which will have the same effect but will be semantically correct.

    Edit2: I think you wanted it in the above way, just triple read your question and I think I gotcha!

    var $p = $('.details p');
    $p.contents()
      .filter(function() { return this.nodeType == 3; }) // Select all textnodes
      .wrap('<p>') // Place them inside paragraph elements
    
    $('br', $p).remove(); // Remove all break elements
    

    Working fiddle: http://jsfiddle.net/garreh/UWDw5/1/

    0 讨论(0)
提交回复
热议问题