01.(function(){
02.
if(!window.JS){
03.
window['JS'] = {}
04.
}
05.
var onReady = function(loadEvent) {
06.
if(!+"v1"){
07.
(function(){
08.
try {
09.
document.documentElement.doScroll("left");
10.
} catch(e) {
11.
setTimeout( arguments.callee, 0 );
12.
return;
13.
}
14.
loadEvent();
15.
})();
16.
}else{
17.
document.addEventListener( "DOMContentLoaded", loadEvent, false );
18.
}
19.
}
20.
JS.onReady = onReady;
21.
var $ = function(id){
22.
return document.getElementById(id);
23.
}
24.
JS.$ = $;
25.})()<!doctype html>
<html dir="ltr" lang="zh-CN">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<title>闭包环境中的事件加载</title>
<script type="text/javascript">
(function(){
if(!window.JS){
window['JS'] = {}
}
var onReady = function(loadEvent) {
if(!+"v1"){
(function(){
try {
document.documentElement.doScroll("left");
} catch(e) {
setTimeout( arguments.callee, 0 );
return;
}
loadEvent();
})();
}else{
document.addEventListener( "DOMContentLoaded", loadEvent, false );
}
}
JS.onReady = onReady;
var $ = function(id){
return document.getElementById(id);
}
JS.$ = $;
})()
JS.onReady(function(){
alert(JS.$("test").innerHTML)
});
JS.onReady(function(){
alert("dddddddddddddddd")
});
</script>
</head>
<body>
<p id="test">Test</p>
</body>
</html>
运行代码