该window.load
函数只会触发一次,当通过 AJAX 将页面拉入 DOM 时,您将在它触发后绑定到它。您的问题的解决方案非常简单,在特定页面初始化时运行代码:
$(document).delegate('#about', 'pageinit', function() {
//notice I start the selection with the current page,
//this way you only run the code on images on this pseudo-page
$(this).find("img.lazy").lazyload();
});
这应该可以正常工作,因为pageinit
直到火灾之后才会document.ready
触发。
另请注意,我使用.delegate()
的,而不是.live()
since.live()
已被贬值,可能会从未来的版本中删除。
$(SELECTION).live(EVENT, EVENT-HANDLER);
是相同的:
$(document).delegate(SELECTION, EVENT, EVENT-HANDLER);
对于奖金回合,从 jQuery 1.7 开始,上述两个函数实际上都映射到.on()
并且您可以.on()
在委托庄园中使用,如下所示:
$(document).on(EVENT, SELECTION, EVENT-HANDLER);
文档:
The reason your code worked on the first page-view was because the window.load
event was firing after you bound to it in the pageinit
event handler, but on subsequent AJAX loads you are binding to the window.load
event even though it won't fire anymore.