0

根据文档,这应该有效:

$('#content').on( 'load', function(event){
    alert('ready')
})

但事实并非如此。http://jsfiddle.net/SJKqt/

这确实有效:

$('#content').on( 'load', alert('ready') )

但我根本无法运行匿名函数。

4

2 回答 2

2

以下元素支持 onload 事件:

<body>, <frame>, <frameset>, iframe, <img>, 
<input type="image">, <link>, <script>, <style>

所以我猜你没有在其中一个上注册处理程序。

于 2012-05-29T22:27:43.043 回答
0

jQuery 页面.load()

与图像一起使用时加载事件的注意事项

开发人员尝试使用 .load() 快捷方式解决的一个常见挑战是在图像(或图像集合)完全加载时执行一个函数。应该注意几个已知的警告。这些都是:

  • 它不能始终如一地工作,也不能可靠地跨浏览器
  • 如果图像 src 设置为与以前相同的 src,它不会在 WebKit 中正确触发
  • It doesn't correctly bubble up the DOM tree
  • Can cease to fire for images that already live in the browser's cache

Note: The .live() and .delegate() methods cannot be used to detect the load event of an iframe. The load event does not correctly bubble up the parent document and the event.target isn't set by Firefox, IE9 or Chrome, which is required to do event delegation.

Your jsFiddle is running in an iframe, which may be one explanation for why your code isn't working there.

于 2012-05-29T22:28:12.590 回答