开发者

jQuery: .filter() returns fewer elements than expected?

开发者 https://www.devze.com 2023-03-05 08:32 出处:网络
开发者_C百科I\'m using PHP and an ajax command to take the entire HTML contents of an external web page (via the PHP file_get_contents() command) and passing that HTML into a javascript variable.

开发者_C百科I'm using PHP and an ajax command to take the entire HTML contents of an external web page (via the PHP file_get_contents() command) and passing that HTML into a javascript variable. With the HTML contents in that variable, I am now trying to select & extract certain elements within that variable. I tried using the .filter() function e.g.

 $(myHTML).filter('#someID').doStuff();

but the .filter() command does not seem to accurately filter my selection.

It works in some cases

$(myHTML).filter('title').text();

yet it does not appear to work when trying to select multiple elements. For example, if I try to get a count of all the <div> or <a> tags, I keep getting zero

alert($(myHTML).filter('a').length)

even though there are 75 <a> tags in this example.

The same happens when trying to get a count of all the <div> tags: the above code just returns zero.

Can anyone explain why this is happening and what I can do to resolve the issue?


Filter will only return the filtered top-level elements, won't traverse children. Use find() instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消