Using jQuery to search a string of HTML

前端 未结 4 1593
小蘑菇
小蘑菇 2020-11-28 14:29

If I run this code -

var html= \'
\'; console.
相关标签:
4条回答
  • 2020-11-28 14:37

    the answer is very simple. when you make object using jQuery(html) then it make an hierarchy of nodes, and when you find some node like 'div', it searches in whole hierachy except root elements, and in your first example, you dont have child 'div' nodes. And in your second example you have only one child 'div' node.

    so if you keep one extra root node in whole hierarchy, then you can find all your nodes easily. like

    var html= '<html><head></head><body><div class="bar"></div></body></html>';
    console.log($('<div></div>').append(html).find('div'));
    
    var html= '<html><head></head><body><div><div class="bar"></div></div></body></html>';
    console.log($('<div></div>').append(html).find('div'));
    
    0 讨论(0)
  • 2020-11-28 14:41

    Let's split this question into two parts.

    First:

    var html= '<html><head></head><body><div class="bar"></div></body></html>';
    console.log($(html).find('div'));
    

    and

    var html= '<html><head></head><body><div><div class="bar"></div></div></body></html>';
    console.log($(html).find('div'));
    

    do not work because according to the jQuery() docs:

    When passing in complex HTML, some browsers may not generate a DOM that exactly replicates the HTML source provided. As mentioned, we use the browser's .innerHTML property to parse the passed HTML and insert it into the current document. During this process, some browsers filter out certain elements such as <html>, <title>, or <head> elements. As a result, the elements inserted may not be representative of the original string passed.

    • In the first code block, your <html>, <head>, and <body> tags are getting stripped out, and <div class="bar"></div> remains. find only searches inside the resulting <div>, and cannot find anything.
    • In the second code block, your <html>, <head>, and <body> tags are getting stripped out, and <div><div class="bar"></div></div> remains. find searches inside the result, and finds a single <div>.

    As for your second part:

    var code = $("<div id='foo'>1</div><div id='bar'>2</div>");
    console.log(code.find('div'));
    

    You first give jQuery a string, which it takes and makes into a jQuery object with the two <div>'s. Then, find searches in each <div>, finds nothing and returns no results.

    Next, in

    var code = $("<div id='foo'>1</div><div id='bar'>2</div>");
    code.each(function() {
        alert( this.nodeName );
    })
    

    each loops through the jQuery object, taking each of the two created <div>'s, and alerts their node name. Therefore, you get two alerts.

    0 讨论(0)
  • 2020-11-28 14:42

    .find in the third example, searches children in each matched element. Inside your divs there are no div children (they don't have any children), so .find(anything) will not return any element.

    .each, on the other hand, iterates over the current elements in the set, which does include the divs (there are two matched elements - the divs).

    As for <html> in your first example, I'm not sure - perhaps you're not allowed to create a second <html> element after the page has loaded. $('<html><head></head><body><div class="bar"></div></body></html>'); only returns the div so .find does not return anything.

    0 讨论(0)
  • 2020-11-28 14:48

    The simple way is as follows:

    Given the string:

    var html= '<html><head></head><body><div class="bar"></div></body></html>';
    

    Search for the div with the class bar:

    $(html).filter('.bar')
    

    Or all divs:

    $(html).filter('div')
    

    Returns the object with the class bar

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