javascrip 自封装document.ready方法
/* *DOM内容加载完毕后执行脚本代码(Dom标签加载完毕后执行代码) * fn:待执行的函数 * scope: fn的执行作用域 */ document.domReady = function (fn, scope) { var doc = document, readyFn = null; //W3C标准 if (doc.addEventListener) { readyFn = function () { doc.removeEventListener('DOMContentLoaded', readyFn, false); fn.apply(scope); }; doc.addEventListener('DOMContentLoaded', readyFn, false); } /*IE下*/ if (doc.attachEvent) { readyFn = function () { if (doc.readyState == 'complete' && !readyFn.done) { readyFn.done = true; fn.apply(scope); doc.detachEvent('onreadystatechange', readyFn); window.detachEvent('onload', readyFn); } }; doc.attachEvent('onreadystatechange', readyFn); window.attachEvent('onload', readyFn); void function () { if (readyFn.done) { return; } try { document.documentElement.doScroll("left"); } catch (e) { setTimeout(arguments.callee, 1); return; } readyFn.done = true; fn.apply(scope); } (); } };