简体   繁体   English

DOMContentLoaded 和 document.onload 事件是否相同?

[英]Is DOMContentLoaded and document.onload events are same?

DOMContentLoaded已加载 DOM 内容

document.addEventListner('DOMContentLoaded',function(){

})

document.onload文件加载

document.onload(function(){

});

Is DOMContentLoaded and document.onload events are same DOMContentLoaded 和 document.onload 事件是否相同

The syntaxe should be (as a comment pointed out) :语法应该是(正如评论指出的那样):

document.onload = function(){};

and I think it does only work when targeting an elemnt like so :我认为它只有在针对这样的元素时才有效:

document.getElementById('image').onload = function(){};

It will assign the function to the onload handler.它将函数分配给 onload 处理程序。

Whereas然而

document.addEventListener('DOMContentLoaded', function(){})

Waits for the DOM to be ready before triggering, meaning that images, iframes, ect... could not be loaded.在触发之前等待 DOM 准备就绪,这意味着无法加载图像、iframe 等。

Could be useful for someone可能对某人有用

source : Difference between DOMContentLoaded and load events source : DOMContentLoaded 和 load 事件之间的区别

DOMContentLoaded==window.onDomReady()

Load==window.onLoad()

A page can't be manipulated safely until the document is "ready."jQuery detects this state of readiness for you.在文档“准备好”之前,无法安全地操作页面。jQuery 会为您检测到这种准备状态。 Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. $( document ).ready() 中包含的代码只会在页面文档对象模型 (DOM) 准备好执行 JavaScript 代码时运行。 Code included inside $( window ).load(function() { ... }) will run once the entire page (images or iframes), not just the DOM, is ready.包含在 $( window ).load(function() { ... }) 中的代码将在整个页面(图像或 iframe)准备好后运行,而不仅仅是 DOM。

See: http://learn.jquery.com/using-jquery-core/document-ready/请参阅: http : //learn.jquery.com/using-jquery-core/document-ready/

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

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