基本上每个库都有这东西,因为如果要对页面上的元素进行操作,我们必须等到页面加载了这个元素才行,否则会报错,但是我们很能判定某个元素是否已加载,但我们可以判定页面是否加载,这就是我们经常把代码放到window.onload = function(){}之中的缘由。但window.onload事件是待到页面上的所有资源被加载才激活,如果页面上有许多图片,音乐或falsh,而我们要操作的元素在的它们的下方呢?因此,W3C做了少有几桩好事,搞了DOMContentLoaded与addEventListener,可能也不是他们搞的,把某浏览器的私有实现盖上个大印,标明它是标准罢了,如safari的canvas,IE的getBoundingClientRect……DOMContentLoaded是DOM树完成时激活的事件,addEventListener支持多重加载与冒泡捕获。本文将在它的基础上进行进一步的封装与改进,如setTimeout改为零秒延迟,清除setTimeout,执行完加载后把加载函数清除掉,对IE框架结构的页面进行更安全的设置……
综合执行顺序为:
1. oncontentready,这时DOM树完成
2. script defer,这时开始执行设定了defer属性的script
3. ondocumentready complete,这时可以使用HTC组件与XHR
4. html.doScroll 这时可以让HTML元素使用doScroll方法
5. window.onload 这时图片flash等资源都加载完毕
// Js 代码
// DOM ready
function domReady(){
new function(){
dom = [];
dom.isReady = false;
dom.isFunction = function(obj){
return Object.prototype.toString.call(obj) === "[object Function]";
}
dom.Ready = function(fn){
dom.initReady();//如果没有建成DOM树,则走第二步,存储起来一起杀
if(dom.isFunction(fn)){
if(dom.isReady){
fn();//如果已经建成DOM,则来一个杀一个
}else{
dom.push(fn);//存储加载事件
}
}
}
dom.fireReady =function(){
if (dom.isReady) return;
dom.isReady = true;
for(var i=0,n=dom.length;i<n;i++){
var fn = dom[i];
fn();
}
dom.length = 0;//清空事件
}
dom.initReady = function(){
if (document.addEventListener) {
document.addEventListener( "DOMContentLoaded", function(){
document.removeEventListener( "DOMContentLoaded", arguments.callee, false );//清除加载函数
dom.fireReady();
}, false );
}else{
if (document.getElementById) {
document.write("<script id=\"ie-domReady\" defer='defer'src=\"//:\"><\/script>");
document.getElementById("ie-domReady").onreadystatechange = function() {
if (this.readyState === "complete") {
dom.fireReady();
this.onreadystatechange = null;
this.parentNode.removeChild(this)
}
};
}
}
}
}
dom.Ready(function(){
alert("我的domReady!")
});
dom.Ready(function(){
alert("我的domReady测试多重加载1!")
});
dom.Ready(function(){
alert("我的domReady测试多重加载2!")
});
dom.Ready(function(){
alert(document.getElementById("test").innerHTML)
});
window.onload = function(){
alert("这是onload事件!")
};
}
// html 代码
<!doctype html>
<html dir="ltr" lang="zh-CN">
<head id="head">
<meta charset="utf-8"/>
<title>jQuery的domReady </title>
<script type="text/javascript" src="common.js">
</script>
</head>
<body>
![图片1](http://img2.imgtn.bdimg.com/it/u=850457477,3991515685&fm=26&gp=0.jpg)
![图片2](http://img0.imgtn.bdimg.com/it/u=3229175216,3077134478&fm=26&gp=0.jpg)
![图片3](http://img1.imgtn.bdimg.com/it/u=1038174750,276046729&fm=26&gp=0.jpg)
![图片4](http://upload-images.jianshu.io/upload_images/6521051-a1b178f3dd5ca405.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![图片5](http://upload-images.jianshu.io/upload_images/6521051-a1b178f3dd5ca405.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<table class="filament_table" cellspacing="0" width="700" rules="cols" >
<col class="grey" width="25%"></col>
<col class="yellow"></col>
<thead>
<tr>
<th>
参数
</th>
<th>
描述
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
scrollbarDown
</td>
<td>
Default. Down scroll arrow is at the specified location
</td>
</tr>
<tr>
<td>
scrollbarHThumb
</td>
<td>
Horizontal scroll thumb or box is at the specified location
</td>
</tr>
<tr>
<td>
scrollbarLeft
</td>
<td>
Left scroll arrow is at the specified location
</td>
</tr>
<tr>
<td>
scrollbarPageDown
</td>
<td>
Page-down scroll bar shaft is at the specified location
</td>
</tr>
<tr>
<td>
scrollbarPageLeft
</td>
<td>
Page-left scroll bar shaft is at the specified location
</td>
</tr>
<tr>
<td>
scrollbarVThumb
</td>
<td>
Vertical scroll thumb or box is at the specified location
</td>
</tr>
<tr>
<td>
down
</td>
<td>
Composite reference to scrollbarDown
</td>
</tr>
<tr>
<td>
left
</td>
<td>
Composite reference to scrollbarLeft
</td>
</tr>
</tbody>
</table>
<p id="test">我们添加了些图片与表格延缓页面的加载速度</p>
![图片1](http://img2.imgtn.bdimg.com/it/u=850457477,3991515685&fm=26&gp=0.jpg)
![图片2](http://img0.imgtn.bdimg.com/it/u=3229175216,3077134478&fm=26&gp=0.jpg)
![图片3](http://img1.imgtn.bdimg.com/it/u=1038174750,276046729&fm=26&gp=0.jpg)
![图片4](http://upload-images.jianshu.io/upload_images/6521051-a1b178f3dd5ca405.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![图片5](http://upload-images.jianshu.io/upload_images/6521051-a1b178f3dd5ca405.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
</body>
</html>