为什么我无法让 jQuery 的 live() 或 load() 工作?

发布于 2024-08-30 06:57:42 字数 277 浏览 4 评论 0 原文

为什么只有第三种方法有效?

$('#jqtest').live('load', function() {$(this).html('hi');}); //1

$('#jqtest').load(function() {$(this).html('hi');}); //2

$(window).load(function() {$('#jqtest').html('hi');}); //3


 <div id="jqtest">kldjfglkj</div>

Why does only the third method work?

$('#jqtest').live('load', function() {$(this).html('hi');}); //1

$('#jqtest').load(function() {$(this).html('hi');}); //2

$(window).load(function() {$('#jqtest').html('hi');}); //3


 <div id="jqtest">kldjfglkj</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

血之狂魔 2024-09-06 06:57:42

您不能在任意选择器上使用 load() 函数;您只能在“与 URL 关联的任何元素:图像、脚本、框架、iframe 和窗口对象”上使用它(文档)。 div 没有关联的 URL,因此前两种技术都不会绑定处理程序。 window 确实有一个 URL,因此它将调用处理程序。

您可能还对 ready() 感兴趣。

You can't use the load() function on arbitrary selectors; you can only use it on "any element associated with a URL: images, scripts, frames, iframes, and the window object" (docs). divs don't have an associated URL, so neither of your first two techniques will bind a handler. window does have a URL, so it will call the handler.

You might also also be interested in ready().

记忆で 2024-09-06 06:57:42

如果您尝试在文档或窗口加载后将 HTML“hi”添加到元素“#jqtest”,那么您就差不多成功了。

$(document).ready(function(){

$("#jqtest").html('hi');

});

当文档加载时,这将更改“#jqtest”的值。您还可以指定ready()函数中的其他事件仅在页面完全加载后执行。

If you're trying to add the HTML "hi" to the element "#jqtest" when the document or window has loaded you're almost there.

$(document).ready(function(){

$("#jqtest").html('hi');

});

This will change the value of "#jqtest" when the document has been loaded. You can also specify other events within the ready() function to only be executed once the page has fully loaded.

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