Can I use jQuery selectors on an HTML string that is not attached to the DOM?

后端 未结 3 1566
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-15 04:48

So if I have a variable like

var ht = \"

Paragraph Here

\"

If it was attached to the DOM I could

3条回答
  •  -上瘾入骨i
    2020-12-15 05:05

    Just wrap your HTML String into a jQuery object, and then you can run your jQuery selectors from there:

    var htmlString = "

    Paragraph Here

    "; var elements = $(htmlString); var p = elements.filter('p').text(); console.log(p); //=> Paragraph Here

    Working demo here.

提交回复
热议问题