If you want an event to work on your page, you should call it inside the $(document).ready() function. Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.

I want to do javascript code only after the page contents are loaded how can I do that?

推荐答案

使用load而不是ready:

$(document).load(function () {
 // code here
});

Update 从jQuery 1.8开始,您需要使用.on().(http://api.jquery.com/on/)

$(window).on('load', function() {
 // code here
});

this answer:

根据http://blog.jquery.com/2016/06/09/jquery-3-0-final-released/:

已删除不推荐使用的事件别名

.load, .unload, and .error, deprecated since jQuery 1.8, are no more. Use .on() to register listeners.

https://github.com/jquery/jquery/issues/2286

Jquery相关问答推荐

bootstrap - Select Select 器不能在下拉菜单中 Select

如何在光滑的轮播上添加进度条

无限滚动jQuery插件

Twitter Bootstrap 是否包含 jQuery?

如何在循环内创建动态变量名称?

了解 Backbone.js REST 调用

Jquery .on('scroll')在滚动时不触发事件

Facebook 风格的 JQuery 自动完成插件

jQuery增量读取AJAX流?

动态设置 iframe src

jQuery UI 滑块(以编程方式设置)

我可以将 Nuget 保留在 jQuery 1.9.x/1.x 路径上(而不是升级到 2.x)吗?

如何使用 jQuery 更改文本

使用 Typescript 时如何停止类型 JQuery 上不存在属性语法错误?

jQuery Select 器中前导冒号的目的是什么?

消除移动 Safari 中点击事件的 300 毫秒延迟

将多个 JavaScript 文件合并为一个 JS 文件

检测 jQuery UI 对话框是否打开

HTMLCollection、NodeLists 和对象数组之间的区别

如何在 jquery 中获取 textarea 的值?