页面加载后如何执行jQuery代码? [英] How to do jquery code AFTER page loading?
问题描述
如果您希望某个事件对您有效 页面上,您应该在 $(document).ready()函数. 里面的所有东西都会尽快加载 在加载DOM之前和之前 页面内容已加载.
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.
我只想在页面内容加载后之后执行javascript代码?
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
});
更新
从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
这篇关于页面加载后如何执行jQuery代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!