If I run this code -
var html= \'\';
console.
Let's split this question into two parts.
First:
var html= '';
console.log($(html).find('div'));
and
var 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
,
, or
elements. As a result, the elements inserted may not be representative of the original string passed.
,
, and
tags are getting stripped out, and
remains. find only searches inside the resulting , and cannot find anything.
- In the second code block, your
,
, and
tags are getting stripped out, and
remains. find
searches inside the result, and finds a single .
As for your second part:
var code = $("12");
console.log(code.find('div'));
You first give jQuery a string, which it takes and makes into a jQuery object with the two 's. Then, find
searches in each , finds nothing and returns no results.
Next, in
var code = $("12");
code.each(function() {
alert( this.nodeName );
})
each loops through the jQuery object, taking each of the two created 's, and alerts their node name. Therefore, you get two alerts.
- 热议问题