window.onload 与 document.ready jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10778070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
window.onload vs document.ready jQuery
提问by KryQ
I have a site with two columns. I want to have equal height on both using jQuery.
我有一个包含两列的站点。我想使用 jQuery 在两者上具有相同的高度。
I'm trying to get the logo column height. I had:
我正在尝试获取徽标列高度。我有:
$(document).ready(function() {
alert($('#logo').height());
});?
and it didn't work. So I changed it to:
它没有用。所以我把它改成:
window.onload = function(){
alert($('#logo').height());
}
And it's working. What is going on in here?
它正在发挥作用。这里发生了什么?
采纳答案by kamesh
I had a same problem in handling Image height and width inside $(document)ready and I found some better referenses to solve it... I hope this may help some one
我在处理 $(document)ready 中的图像高度和宽度时遇到了同样的问题,我找到了一些更好的参考来解决它......我希望这可以帮助某人
$(document).ready()
$(文档).ready()
The document ready event fired when the HTML document is loaded and the DOM is ready, even if all the graphics haven't loaded yet. If you want to hook up your events for certain elements before the window loads, then $(document).ready is the right place.
当 HTML 文档加载完毕且 DOM 准备就绪时,即使所有图形尚未加载,也会触发文档就绪事件。如果您想在窗口加载之前为某些元素连接事件,那么 $(document).ready 是正确的位置。
Code:
代码:
$(document).ready(function() {
// document is loaded and DOM is ready
alert("document is ready");
});
$(window).load()
$(window).load()
The window load event fired a bit later, when the complete page is fully loaded, including all frames, objects and images. Therefore functions which concern images or other page contents should be placed in the load event for the window or the content tag itself.
窗口加载事件稍后触发,当整个页面完全加载时,包括所有框架、对象和图像。因此,与图像或其他页面内容有关的函数应放置在窗口的加载事件或内容标签本身中。
Code:
代码:
$(window).load(function() {
// page is fully loaded, including all frames, objects and images
alert("window is loaded");
});
回答by ahren
document ready
is fired when the DOM has loaded, so information like height isn't available, unless it's explicitly declared.
document ready
当 DOM 加载时被触发,所以像高度这样的信息不可用,除非它被明确声明。
window onload
waits for the assets in the page to be completely loaded - so information such as height is now available.
window onload
等待页面中的资产完全加载 - 因此现在可以使用诸如高度之类的信息。