我对 jquery 的负载感到困惑

发布于 2024-11-08 06:06:49 字数 555 浏览 0 评论 0原文

使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

雪化雨蝶 2024-11-15 06:06:49

如果我理解正确的话,你想在 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 here

It 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.

末が日狂欢 2024-11-15 06:06:49

在您的第一个代码中,您正在查找一个 #google 元素,并告诉它在加载图像时通知您。但是,在执行 javascript 时(代码中的元素上方),不存在 #google 元素,所以事件监听器没有分配给任何东西。

遗憾的是,在 DOMReady 侦听器中添加事件侦听器也并不总能解决问题。例如,如果图像已在浏览器缓存中,则 #google 加载事件可能会在 DOMReady 之前发生。最安全的方法似乎是在创建图像后立即附加侦听器。

<img src="http://www.google.com/images/logos/ps_logo2.png" id="google" />
<script type="text/javascript">

$('#google').load(function() {
   alert('ok');
});

</script>

工作示例

如果您不满意在代码中间注入脚本块,但是如果您想将脚本推迟到稍后的某个地方,您可以随时监听 $(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 before DOMReady. The safest approach seems to be to attach the listener immediately after the image is created.

<img src="http://www.google.com/images/logos/ps_logo2.png" id="google" />
<script type="text/javascript">

$('#google').load(function() {
   alert('ok');
});

</script>

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.

各自安好 2024-11-15 06:06:49

您应该首先将事件处理程序绑定到加载事件,然后设置图像的 src 以实际启动加载。

$('#google').bind("load", function() {
    alert('ok');
});
$("#google").attr("src", "http://www.google.com/images/logos/ps_logo2.png");

看到这个: 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.

$('#google').bind("load", function() {
    alert('ok');
});
$("#google").attr("src", "http://www.google.com/images/logos/ps_logo2.png");

see this: http://jsfiddle.net/VaLhf/

故事和酒 2024-11-15 06:06:49

尝试这样做:

$(function() {
    var that = $("#google");
    alert(that);
});

此代码将在 DOM 完全加载时运行,由 $(document) ready() 事件。

Try this instead:

$(function() {
    var that = $("#google");
    alert(that);
});

This code will run when the DOM is fully loaded, as triggered by the $(document) ready() event.

岛徒 2024-11-15 06:06:49

只需使用以下内容:

$(document).ready(function() {
   alert("The DOM has loaded");
});

.load 函数用于触发 ajax 调用并将响应放置在指定的 div 中。

Just use the following:

$(document).ready(function() {
   alert("The DOM has loaded");
});

The .load function is used to trigger an ajax call and place the response in the div specified.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文