javascript $(window).load() 在 IE 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9651012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:20:48  来源:igfitidea点击:

$(window).load() in IE?

javascriptjqueryinternet-explorer

提问by u283863

Recently I ran into a mysterious problem that IE (6-8) is keeping throwing me an error. I don't know if this is the problem, but I think it is.

最近我遇到了一个神秘的问题,即 IE (6-8) 一直在向我抛出错误。我不知道这是不是问题,但我认为是。

Open up the F12 developer tools in a jQuery included website, enter

在包含jQuery 的网站中打开 F12 开发人员工具,输入

$(window).load(function(){
     alert("Wont able to see me");
});

And an error will popup:

并且会弹出一个错误:

"Unable to get value of the property 'slice': object is null or undefined"

“无法获取属性‘切片’的值:对象为空或未定义”

Did I do anything wrong, or anything else???

我做错了什么,还是别的什么???

采纳答案by Ekin Koc

The latest jQuery (1.7.1) with IE10 and IE9 does not produce such an error for me.

带有 IE10 和 IE9 的最新 jQuery (1.7.1) 不会对我产生这样的错误。

As a side note;If you wish to execute something when the dom is ready; Try this way;

作为旁注;如果你想在 dom 准备好时执行一些事情;试试这个方法;

$(function(){
     alert("Wont able to see me");
});

I believe this is the standard convention for attaching a function to domready event.

我相信这是将函数附加到 domready 事件的标准约定。

Reference: jQuery Documentation

参考:jQuery 文档

回答by Jovino

I recently found a work-around for IE not recognizing $(window).load()...

我最近找到了 IE 无法识别的解决方法$(window).load()......

window.onload = function() {
    alert("See me, hear me, touch me!");
};

This is a little different than $(function(){})as it executes after all elements are loaded as opposed to when the DOM is ready.

这与$(function(){})在加载所有元素后执行而不是在 DOM 准备好时执行有点不同。

I recently implemented this in another project and it worked wonderfully.

我最近在另一个项目中实现了这个,并且效果很好。

回答by JohnnyK

For anyone still running into this, IE11 (only one I tested) does not fire the the load event if the listener is inside of the jquery ready function. So pull the load function outside of the ready function and it will fire in IE11.

对于仍然遇到此问题的任何人,如果侦听器位于 jquery 就绪函数内,则 IE11(我仅测试过一个)不会触发 load 事件。因此,将 load 函数拉到 ready 函数之外,它将在 IE11 中触发。

//this is bad
$(() => { //jquery ready
    window.onload = () => { //wont fire in IE
        cosole.log('window loaded'); 
    }
});

//this is good
$(() => { //jquery ready
    cosole.log('dom ready'); 
});

window.onload = () => { //will fire in IE
    cosole.log('window loaded'); 
}