jQuery - wrap all unwrapped text in p tags

前端 未结 5 466
醉梦人生
醉梦人生 2020-12-02 02:21

I have a situation where the following code is getting written to my page.

Some text here which is not wrapped in tags

Some mor

5条回答
  •  执念已碎
    2020-12-02 02:55

    Ran into a similar need and attempted to employ the solution @Arash_Milani. Solution worked, however I ran into conflicts when the page also required to make ajax calls.

    After a bit of digging, I found a fairly straight-forward solution on api.jquery.com using the .contents() method:

    $('.wrapper').contents().filter(function() {
      return this.nodeType === 3;
    }).wrap('

    ').end();
    p.good {
      color: #09f;
    }
    p.fixed {
      color: #ff0000;
      text-align: center;
    }
    
    
    Some plain text not wrapped in any html element.

    This text is properly wrapped in a paragraph element.

提交回复
热议问题