jQuery $("body").ready() 有效但 $("body").load() 无效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4559272/
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
$("body").ready() works but $("body").load() does not work?
提问by user855
Why? I'm having the same issue with the image Tag.
为什么?我对图像标签有同样的问题。
ready() callback gets invoked. load() callback never gets invoked.
ready() 回调被调用。load() 回调永远不会被调用。
Browser: Firefox3.6.8 on Mac
浏览器:Mac Firefox3.6.8
Edit:
编辑:
I somehow get the feeling that I am using load() incorrectly in JQuery. Documentation I am referring to:- http://api.jquery.com/load-event/
我以某种方式感觉到我在 JQuery 中错误地使用了 load()。我指的文档:- http://api.jquery.com/load-event/
I am doing
我在做
$("body").load(function() { // do something });
$("body").load(function() { // 做某事 });
Isn't this right? I see some code doing:- $("#id").load("./newpage.html"); But these 2 are different APIs right?
这不对吗?我看到一些代码在做:- $("#id").load("./newpage.html"); 但是这两个是不同的 API 对吗?
Edit 2
编辑 2
Some more code to explain my whole issue here:-
还有一些代码可以在这里解释我的整个问题:-
var tagString = "<img id='"+imageId+"'></img>";
this.divElem.append(tagString);
var imgElems = $("#"+imageId);
var vowels = this;
imgElems.each(function (index) {
$(this).attr('id',imgId).attr('src',imageUrl)
.attr('width',1).attr('height',1);
$(this).load(function() {
// do something.
// This Damned! function is never getting called!
});
});
Works
作品
$().ready(function() {
$().load(function() {
/// something. this worked!
});
});
Does not work
不起作用
// without the ready() wrapper does not work
$().load(function() {
/// something. this worked!
});
Why? I am happy it works. But I don't understand why!
为什么?我很高兴它有效。但我不明白为什么!
-Ajay
-阿杰
回答by Guffa
As in the example on the page that you linked to, use:
在您链接到的页面上的示例中,使用:
$(window).load(function(){
// do something
});
It might be possible to bind the event to the body element like you tried, but then you have to place the code for doing that inside the body element. If you put the script in the head (where script preferrably goes), the body element does't exist yet when the code runs.
可以像您尝试的那样将事件绑定到 body 元素,但是您必须将执行此操作的代码放置在 body 元素中。如果您将脚本放在头部(脚本最好放在那里),则在代码运行时 body 元素还不存在。
回答by Nick Craver
Technically, $("anything").ready()
works...it doesn't even look at the selector, though an erroneous selector will error there. With .load()
it's an event handler, so you need to make sure you aren't binding afterthe event has fired, or you're listening for an event that already happened.
从技术上讲,$("anything").ready()
有效......它甚至不看选择器,尽管错误的选择器会在那里出错。随着.load()
这是一个事件处理程序,所以你需要确保你是不是绑定后的事件已触发,或者你是一个事件监听已经发生了。
回答by Nosh
.ready() and .load() are completely different.
.ready() 和 .load() 是完全不同的。
$("body").ready(onBodyLoaded);
refer https://api.jquery.com/ready/
参考https://api.jquery.com/ready/
This line tells the browser call the function onBodyLoaded when the body has been loaded.
该行告诉浏览器在加载正文时调用 onBodyLoaded 函数。
var url = some url
var data = {}
$("body").load( url , data, onComplete(responseText, textStatus, XMLHttpRequest) )
This tells the browser to request the url, passing data if POST, into the body, and then call the onComplete function.
这告诉浏览器请求 url,如果 POST 则将数据传递到正文中,然后调用 onComplete 函数。