为什么我无法让 jQuery 的 live() 或 load() 工作?
为什么只有第三种方法有效?
$('#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在任意选择器上使用
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).div
s 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().
如果您尝试在文档或窗口加载后将 HTML“hi”添加到元素“#jqtest”,那么您就差不多成功了。
当文档加载时,这将更改“#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.
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.