简体   繁体   English

Firefox、Jquery 和缺少的 dom 元素

[英]Firefox, Jquery and missing dom elements

I've come across a problem when trying to loop through all divs on a page.我在尝试遍历页面上的所有 div 时遇到了问题。

I can grab all the divs using $('div') in IE but not in firefox.我可以在 IE 中使用 $('div') 获取所有 div,但不能在 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 :- IE 中的输出是:-

29 29 29 29

The output in firefox is :- Firefox 中的输出是:-

29 1 29 1

Am I missing something?我错过了什么吗?

Check your markup.检查您的标记。 With 29 divs, I bet you have a missing </div> or something somewhere.有 29 个 div,我敢打赌你缺少</div>或某处的东西。 IE might just count the opening tag; IE 可能只计算开始标签; perhaps FF is more strict.也许FF更严格。

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.如果不是这样,请确保您已关闭 Firebug - 或任何其他可能修改 DOM 的东西 - 关闭。

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

$("div") creates new div element, that's why 1 ;) $("div") 创建新的 div 元素,这就是为什么 1 ;)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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