简短版
在调用jQuery.after
来插入HTML之后,我怎么知道DOM何时准备就绪?
长版
我从ajax调用中获取Plupload dropzones并使用jQuery.after来插入它们。当我然后调用mypluploader.init()
初始化Plupload时,由于DOM没有准备好(显然),它失败了。这就是我正在做的事情:
mypluploader.init()
初始化Plupload TypeError: Cannot read property 'style' of null
摘要代码:
$.get('/plupload-zones.html', function(theDropZoneHTML) {
// theDropZoneHTML: <div id='dropzones'>...</div>
obj.after(theDropZoneHTML);
mypluploader = new plupload.Uploader({..., drop_element: 'dropzones', ...});
mypluploader.init();
//Boom! It fails
});
如果我更改步骤4以包含setTimeout 2秒,它可以正常工作,没有错误:
$.get('/plupload-zones.html', function(theDropZoneHTML) {
// theDropZoneHTML: <div id='dropzones'>...</div>
obj.after(theDropZoneHTML);
setTimeout(function() {
mypluploader = new plupload.Uploader({..., drop_element: 'dropzones', ...});
mypluploader.init();
//Bingo! It works
}, 2000);
});