window.onload
2020-12-04 20:34
标签:style blog class code java ext 执行结果是null。window.onload = haha();
这样写是错误的,不需要加括号,引用不用加括号,加了括号就成了函数返回值了,在页面还没加载完就执行了,当然是null。window.onload
= haha; 这样才是正确的写法。 执行结果是:DOMContentLoaded然后才是 如果你是个jQuery使用者,你可能会经常使用$(document).ready();或者$(function(){})
这都是使用了DOMContentLoaded事件 window.onload,搜素材,soscw.com window.onload 标签:style blog class code java ext 原文地址:http://www.cnblogs.com/EricaMIN1987_IT/p/3705794.htmlhtml xmlns="http://www.w3.org/1999/xhtml">
head>
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
title>title>
script type="text/javascript" src="jquery2.js">script>
script language="javascript">
window.onload = haha();
function haha(){console.log(document.getElementById("div1"));}
script>
head>
body>
div id="div1">adiv>
body>
html>
html xmlns="http://www.w3.org/1999/xhtml">
head>
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
title>title>
script type="text/javascript" src="jquery2.js">script>
script language="javascript">
window.onload = haha;
function haha(){console.log(document.getElementById("div1"));}
if(document.addEventListener){
function DOMContentLoaded(){
console.log("DOMContentLoaded");
}
document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
}
script>
head>
body>
div id="div1">adiv>
body>
html>
http://bbs.csdn.net/topics/340107576