Using .text() to retrieve only text not nested in child tags

后端 未结 25 1705
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 05:55

If I have html like this:

  • This is some text First span text
  • 25条回答
    •  挽巷
      挽巷 (楼主)
      2020-11-21 06:42

      Use an extra condition to check if innerHTML and innerText are the same. Only in those cases, replace the text.

      $(function() {
      $('body *').each(function () {
          console.log($(this).html());
          console.log($(this).text());
          if($(this).text() === "Search" && $(this).html()===$(this).text())  {
              $(this).html("Find");
          }
      })
      })
      

      http://jsfiddle.net/7RSGh/

    提交回复
    热议问题