我遇到一个问题,我试图使用jQuery将一些html加载到页面上。'get'加载后的jQuery调用函数
内容本身加载正常,但我想要做的是使用jquery ui使该内容“可拖动”。
我遇到的问题是,当我调用该函数使内容可拖动时,它尚未加载,因此没有任何内容可拖动。
我尝试了一些成功,在函数调用之前添加了一个setTimeout,这让它工作,但不是我想怎么样。 setTimeout不适用于缓慢的互联网连接,或者当网站缓慢加载,因为它太低。但是如果我把它设置的太高,那么在内容可移动之前还有很长的等待时间。
我想知道在调用函数之前是否有方法检测内容是否已经加载?
我的代码:
$(".naviitem").click(function(){ //When icon is clicked
var i = $(this).attr('id'); //Get id of clicked icon
$.when(load(i)).then(makeDrag()); //Load page then call makeDrag (Doesn't function properly)
});
function load(i){
$.get('wnd/' + i + '.htm', {}, function(data) { //This all works
var $response = $('<div />').html(data);
var $load = $response.find('#p' + i);
$('#page').append($load);
},'html');
}
function makeDrag(){
//does something here
}
试着做''load'返回$ .get'。像:'function load(i){return $ .get(...}' – 2012-04-02 17:38:15