Javascript 在页面完全加载后做一些事情
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9036969/
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
Do something AFTER the page has loaded completely
提问by eozzy
I'm using some embed codes that insert HTML to the page dynamically and since I have to modify that dynamically inserted HTML, I want a jquery function to wait until the page has loaded, I tried delay
but it doesnt seem to work.
我正在使用一些嵌入代码将 HTML 动态插入到页面中,并且由于我必须修改动态插入的 HTML,我想要一个 jquery 函数等待页面加载,我尝试过delay
但它似乎不起作用。
So for example, the dynamically inserted HTMl has an element div#abc
例如,动态插入的 HTMl 有一个元素 div#abc
and I have this jquery:
我有这个 jquery:
if ( $('#abc')[0] ) {
alert("yes");
}
the alert doesn't show up.
警报没有出现。
I'd appreciate any help
我很感激任何帮助
Thanks
谢谢
回答by Zoltan Toth
$(window).load(function () {
....
});
If you have to wait for an iframe (and don't care about the assets, just the DOM) - try this:
如果您必须等待 iframe(并且不关心资产,只关心 DOM) - 试试这个:
$(document).ready(function() {
$('iframe').load(function() {
// do something
});
});
回答by James Hill
That is the purpose of jQuery's .ready()
event:
这就是jQuery.ready()
事件的目的:
$(document).ready(function() {
if ( $('#abc').length ) //If checking if the element exists, use .length
alert("yes");
});
Description: Specify a function to execute when the DOM is fully loaded.
描述:指定在 DOM 完全加载时要执行的函数。
回答by Juri
Using the jQuery.readyshould be enough. Try this
使用jQuery.ready应该就足够了。尝试这个
$(document).ready(function(){
//your code here
});
or
或者
$(function(){
});
which is a shortcut of the first.
这是第一个的捷径。
回答by DJ Quimby
Try this:
尝试这个:
$(document).ready(function () {
if ( $('#abc')[0] ) {
alert("yes");
}
});
回答by Atiq Baqi
The load()
method was deprecated in jQuery version 1.8 and removed in version 3.0.
So you have to use -
该load()
方法在 jQuery 1.8 版中已弃用,并在 3.0 版中删除。所以你必须使用 -
$(window).on('load', function() {
// code here
});
回答by Stefan
$(window).load(function () { ... }
can be enough but otherwise your embeded code (what ever that can be) might provide some callback functionality that you can make use of.
可能就足够了,否则您的嵌入代码(可能是什么)可能会提供一些您可以使用的回调功能。
delay()should only be used to delay animations.
回答by allenski
Generally, to handle my JQuery before or after page loads, will use:
通常,要在页面加载之前或之后处理我的 JQuery,将使用:
jQuery(function($){
// use jQuery code here with $ formatting
// executes BEFORE page finishes loading
});
jQuery(document).ready(function($){
// use jQuery code here with $ formatting
// executes AFTER page finishes loading
});
回答by Muhammad Nasir Shamshad
Make sue you bind the event with dom load so it's there when trigger called. This is how you do it. Hope this helps someone someday
确保您将事件与 dom load 绑定在一起,以便在调用触发器时它就在那里。这就是你如何做到的。希望有一天这对某人有所帮助
$(window).bind("load", function() {
//enter code here
$("#dropdow-id").trigger('change');
});`