简体   繁体   中英

Firefox, Jquery and missing dom elements

I've come across a problem when trying to loop through all divs on a page.

I can grab all the divs using $('div') in IE but not in firefox.

I did the following as a test:-

$(function () {
    var divs = document.getElementsByTagName('div');
    alert(divs.length)
    var divs2 = $('div')
    alert(divs2.length)
});

The output in IE is :-

29 29

The output in firefox is :-

29 1

Am I missing something?

Check your markup. With 29 divs, I bet you have a missing </div> or something somewhere. IE might just count the opening tag; perhaps FF is more strict.

I agree that you probably have a markup problem. If it's not that, make sure that you have Firebug - or anything else that might modify the DOM - turned off.

What about var divs2 = $('div'). length ; var divs2 = $('div'). length ;

$("div") creates new div element, that's why 1 ;)

Try: alert($("div").length);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM