我对 jquery 的负载感到困惑
使用jquery,我如何在dom加载后做一些事情,我这样写:
<script>
$("#google").load(function() {
alert('ok');
});
</script>
<img src="http://www.google.com/images/logos/ps_logo2.png" id="google" />
但什么也没发生~ 如果我使用ready()而不是这样的load:,
<script>
$("#google").ready(function() {
alert(this);
alert('ok');
});
</script>
<img src="http://www.google.com/images/logos/ps_logo2.png" id="google" />
它会工作,但是指针“this”不会是$(“#google”),它似乎是“窗口”或其他东西,我感到困惑〜
with jquery,how can i do something just after the dom loaded,i write like this:
<script>
$("#google").load(function() {
alert('ok');
});
</script>
<img src="http://www.google.com/images/logos/ps_logo2.png" id="google" />
but nothing happen~
if i use ready() instead of load like this:
<script>
$("#google").ready(function() {
alert(this);
alert('ok');
});
</script>
<img src="http://www.google.com/images/logos/ps_logo2.png" id="google" />
,it will work ,but the pointer "this" will not be $("#google"),and it seems to be "window" or other things,i feel confused~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果我理解正确的话,你想在 DOM 完全加载后操作
#google
。那么你要使用的其实是$(document).ready(function() {//do stuff with #google});
.load()
用来发出 ajax 请求。您应该在此处阅读它。了解 #google 是否是动态生成的也很有趣。
哦,在你的第二个例子中,你不能执行
alert(this)
。alert()
仅接受一个字符串作为参数。If I understood correctly you want to manipulate
#google
after the DOM is fully loaded. Then what you want to use is actually$(document).ready(function() {//do stuff with #google});
.load()
is used to make ajax requests. You should read up on it hereIt would also be interesting to know whether #google is dynamically generated or not.
Oh and in your second example, you can't do
alert(this)
.alert()
just takes a string as a parameter.在您的第一个代码中,您正在查找一个
#google
元素,并告诉它在加载图像时通知您。但是,在执行 javascript 时(代码中的元素上方),不存在#google
元素,所以事件监听器没有分配给任何东西。遗憾的是,在 DOMReady 侦听器中添加事件侦听器也并不总能解决问题。例如,如果图像已在浏览器缓存中,则
#google
加载事件可能会在DOMReady
之前发生。最安全的方法似乎是在创建图像后立即附加侦听器。工作示例
如果您不满意在代码中间注入脚本块,但是如果您想将脚本推迟到稍后的某个地方,您可以随时监听
$(window).load()
而不是$('#google').load()
。这相当于,即当所有外部内容加载完毕时它将运行。因此,当
$(window).load()
发生时,您知道您的#google
图片已加载,但不能保证立即加载后。In your first code, you're looking a
#google
element and tells it to notify you when its image is loaded. However, at the time where the javascript is executed (above the element, in the code), there exists no#google
element, and so the event listener is not assigned to anything.Sadly, adding the event listener inside a
DOMReady
listener won't always solve the problem either. If, for instance, the image is already in browser cache, the#google
load event may occur beforeDOMReady
. The safest approach seems to be to attach the listener immediately after the image is created.Working example
If you're not happy with injecting a script block in the middle of your code like that, but want to defer your script to someplace later, you can always listen to
$(window).load()
instead of$('#google').load()
. This is equivalent to<body onload="">
, i.e. it will run when all external content has been loaded. So when$(window).load()
occurs, you know that your#google
image has loaded, but it's not guaranteed to be immediately after it's loaded.您应该首先将事件处理程序绑定到加载事件,然后设置图像的 src 以实际启动加载。
看到这个: http://jsfiddle.net/VaLhf/
You should first bind the event handler to the load event, then set the image's src to actually start the load.
see this: http://jsfiddle.net/VaLhf/
尝试这样做:
此代码将在 DOM 完全加载时运行,由
$(document)
ready() 事件。Try this instead:
This code will run when the DOM is fully loaded, as triggered by the
$(document)
ready() event.只需使用以下内容:
.load 函数用于触发 ajax 调用并将响应放置在指定的 div 中。
Just use the following:
The .load function is used to trigger an ajax call and place the response in the div specified.