且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

页面加载后如何执行jQuery代码?

更新时间:2023-12-04 22:20:10

使用load代替ready:

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

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

Update You need to use .on() since jQuery 1.8. (http://api.jquery.com/on/)

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

来自此答案:

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

已删除不赞成使用的事件别名

从jQuery 1.8开始不推荐使用的

.load.unload.error 更多的.使用.on()注册侦听器.

Removed deprecated event aliases

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

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