Find element at HTML string

后端 未结 2 1922
北荒
北荒 2020-12-16 11:56

Let\'s suppose I have

var testHTML = \"
\";

I want to get above div as JQuery object

相关标签:
2条回答
  • 2020-12-16 12:21

    You could use the .search() function of the javascript itself.

    testHTML.search('demo_div')>-1
    
    0 讨论(0)
  • 2020-12-16 12:24

    The problem is that your div is the root element in the collection, so you need to use .filter() instead of .find() (which only looks at descendant elements):

    var found = $(testHTML).filter("#demo_div");
    

    I'm assuming your actual HTML string is more complex than the one in the question, otherwise you don't need to do anything other than $(testHTML) and you will have a jQuery object containing that single element.

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