jquery: how can you select texts not surrounded by html tags?

后端 未结 7 2213
Beer

Vodka
rum
whiskey

how can you select beer ? or rum ? in jquery ? they are not surrounded by any html tags....

7条回答
  •  不要未来只要你来
    2021-01-18 16:20

    Personally, I'd wrap them in a then you can refer to them more easily. I'm using Russ Cam's filter, so props (and +1) to him

    $(document).ready(function(){
     $('body').contents().filter(function(){ return this.nodeType === 3 }).each(function(){
      $(this).wrap('');
     })
    })
    

    Then you can just use $('.text') and selectors to access them more easily.

提交回复
热议问题