-
Notifications
You must be signed in to change notification settings - Fork 23
/
23_ready兼容处理.html
35 lines (31 loc) · 1.03 KB
/
23_ready兼容处理.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
function ready(callback) {
if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', function(e) {
callback.call(null, e);
}, false);
} else if (document.attachEvent) {
// 不支持的默认为IE8及以下
document.attachEvent('onreadystatechange', function(e) {
if (document.readyState === 'complete') {
document.detachEvent('onreadystatechange');
callback.call(null, e);
}
});
}
}
// 使用document.readyState==='complete'来判断完成在IE678上可能会出现问题,当页面有张巨大的图时,用此判断的结果和onload事件一样
ready(function(e) {
console.log('ready事件已经触发');
console.log('事件对象为', e);
});
</script>
</body>
</html>