为什么 $(window).load() 在 jQuery 中不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38650315/
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-08-26 17:11:53  来源:igfitidea点击:

why $(window).load() is not working in jQuery?

jquery

提问by Shams Nahid

I'm learning jQuery using visual studio and testing my code in Chrome browser. This is my HTML code

我正在使用 Visual Studio 学习 jQuery 并在 Chrome 浏览器中测试我的代码。这是我的 HTML 代码

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="jquery-3.1.0.js"></script>

    <script type="text/javascript">
        $(window).load(function () {
            alert("Window Loaded");
        });
    </script>
</head>
<body>

</body>
</html>

This is my solution explorer

这是我的解决方案浏览器

Solution Explorer

解决方案资源管理器

Now why my browser doesn't alert "window Loaded"?

现在为什么我的浏览器没有提醒“窗口已加载”?

回答by Bhojendra Rauniyar

You're using jQuery version 3.1.0 and the load event is deprecated for use since jQuery version 1.8. The load event is removed from jQuery 3.0. Instead you can use onmethod and bind the JavaScript load event:

您使用的是 jQuery 3.1.0 版,并且自 jQuery 1.8 版起不推荐使用 load 事件。load 事件已从jQuery 3.0 中删除。相反,您可以使用on方法并绑定 JavaScript加载事件

 $(window).on('load', function () {
      alert("Window Loaded");
 });

回答by Gene

<script type="text/javascript">
   $(window).ready(function () {
      alert("Window Loaded");
   });
</script>

回答by William Hou

I have to write a whole answer separately since it's hard to add a comment so long to the second answer.

我必须单独写一个完整的答案,因为很难在第二个答案中添加这么长的评论。

I'm sorry to say this, but the second answer above doesn't work right.

我很抱歉这么说,但上面的第二个答案不起作用。

The following three scenarios will show my point:

以下三个场景将说明我的观点:

Scenario 1:Before the following way was deprecated,

场景1:在以下方式被弃用之前,

  $(window).load(function () {
     alert("Window Loaded.");
  });

if we execute the following two queries:

如果我们执行以下两个查询:

<script>
   $(window).load(function () {
     alert("Window Loaded.");
   }); 

   $(document).ready(function() {
     alert("Dom Loaded.");
   });
</script>,

the alert (Dom Loaded.) from the second query will show first, and the one (Window Loaded.) from the first query will show later, which is the way it should be.

第二个查询中的警报 (Dom Loaded.) 将首先显示,第一个查询中的警报 (Window Loaded.) 将稍后显示,这应该是这样的。

Scenario 2:But if we execute the following two queries like the second answer above suggests:

场景 2:但是如果我们像上面的第二个答案一样执行以下两个查询:

<script>
   $(window).ready(function () {
     alert("Window Loaded.");
   }); 

   $(document).ready(function() {
     alert("Dom Loaded.");
   });
</script>,

the alert (Window Loaded.) from the first query will show first, and the one (Dom Loaded.) from the second query will show later, which is NOT right.

第一个查询中的警报 (Window Loaded.) 将首先显示,第二个查询中的警报 (Dom Loaded.) 将稍后显示,这是不对的。

Scenario 3:On the other hand, if we execute the following two queries, we'll get the correct result:

场景 3:另一方面,如果我们执行以下两个查询,我们将得到正确的结果:

<script>
   $(window).on("load", function () {
     alert("Window Loaded.");
   }); 

   $(document).ready(function() {
     alert("Dom Loaded.");
   });
</script>,

that is to say, the alert (Dom Loaded.) from the second query will show first, and the one (Window Loaded.) from the first query will show later, which is the RIGHTresult.

也就是说,第二个查询的警报(Dom Loaded.)将首先显示,第一个查询的警报(Window Loaded.)将稍后显示,这是正确的结果。

In short, the FIRST answer is the CORRECT one:

简而言之,第一个答案是正确的:

$(window).on('load', function () {
  alert("Window Loaded.");
});

回答by Mohsin Shoukat

in jquery-3.1.1

在 jquery-3.1.1 中

$("#id").load(function(){
//code goes here});

will not work because load function is no more work

将无法工作,因为加载功能不再工作