Find text in element that is NOT wrapped in html tags and wrap it with

前端 未结 1 333
后悔当初
后悔当初 2021-01-13 19:44

Lorem Ipsum

TEXT THAT NEEDS TO BE WRAPPED
  • List Item 1
相关标签:
1条回答
  • 2021-01-13 20:33

    Use contents() and filter() to get text node

    $('.menu-content')
      .contents() // get all child node including text and comment 
      .filter(function() { // filter the text node which is not empty
        return this.nodeType === 3 && $.trim(this.textContent).length
      }).wrap('</p>'); // wrap filtered element with p
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="menu-content">
      <h3>Lorem Ipsum</h3>
      TEXT THAT NEEDS TO BE WRAPPED
      <ul>
        <li>List Item 1</li>
      </ul>
    </div>

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