Dom ready

作者: LabRaDor2079 | 来源:发表于2017-07-13 08:13 被阅读0次
基本上每个库都有这东西,因为如果要对页面上的元素进行操作,我们必须等到页面加载了这个元素才行,否则会报错,但是我们很能判定某个元素是否已加载,但我们可以判定页面是否加载,这就是我们经常把代码放到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](https://img.haomeiwen.com/i6521051/a1b178f3dd5ca405.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![图片5](https://img.haomeiwen.com/i6521051/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](https://img.haomeiwen.com/i6521051/a1b178f3dd5ca405.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![图片5](https://img.haomeiwen.com/i6521051/a1b178f3dd5ca405.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
</body>
</html>








相关文章

  • js : RegExp

    $(document).ready和window.onload的区别 $(document).ready DOM结...

  • Dom ready

    基本上每个库都有这东西,因为如果要对页面上的元素进行操作,我们必须等到页面加载了这个元素才行,否则会报错,但是我们...

  • DOM Ready

    认识 DOM 认识DOMDOM,全程 Document Object Model,也就是我们常说的文档对象模型。D...

  • jQuery动画与ajax

    题目1: jQuery 中, $(document).ready()是什么意思? .ready()指的是当DOM准...

  • js代码的执行顺序

    关于$(function(){ }), window.onload,$(document).ready(),DOM...

  • DOM ready原理

    DOM ready这个已经有很多人说了,说的也很详细,这里简单说下。利用问题的形式展开~ 问:为什么需要dom r...

  • 第 6 章 jQuery 事件与应用

    页面加载时触发 ready() 事件 ready()事件类似于onLoad()事件,但前者只要页面的 DOM 结构...

  • jquery-2

    $(document).ready() 当dom完全加载(例如html被完全解析dom树构建完成时),jquery...

  • 前端笔记01

    day01 1. window.onload和window.ready区别:ready只是dom结构加载完毕,便视...

  • jQuery学习小结2(jQueryHTML)

    //获得DOM中的文本内容$(document).ready(function(){$("#button").cl...

网友评论

      本文标题:Dom ready

      本文链接:https://www.haomeiwen.com/subject/vpaihxtx.html