jQuery - selectors on a html string

后端 未结 4 762
野的像风
野的像风 2020-12-14 15:36

I am going to get a element from a html-string with jQuery, but I always get an undefined in my console. My String is:

asd         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 15:59

    You can't use jQuery selectors on a collection of nodes like this. You can wrap in a single node to work around this, so just add e.g. a wrapping node.

    var htmlBits = ''
      + 'asdfghjkl'
      + '';
    

    And then it will work:

    console.log($('.test', htmlBits).text()); // outputs 'asd'
    

    JSFiddle (remember to view the console to see the log).

提交回复
热议问题