0
爲了只加載在觸摸設備的jQuery的刷卡劇本,我與近代化檢測,並追加劇本,如果是觸摸,就像這樣:動態添加.js文件,如何知道它何時準備好?
/**/
if (modernizer.touch) {
var js = jQuery("<script>");
css.attr({
type: "text/css",
src: base_url + "/js/jquery-touch-swipe.js?p1"
});
$("head").append(css);
$this.swipe(function(e, dx, dy){
if(dx < 0){
$this.find('.next').click();
} else {
$this.find('.prev').click();
}
});
}
的問題是,經過正確的功能swipe
ISN尚未準備好。我可以使用setTimeout
,但我無法確定,
如何確保附加腳本已準備就緒?
我想是這樣的:
if (modernizer.touch) {
var js = jQuery("<script>");
css.attr({
type: "text/css",
src: base_url + "/js/jquery-touch-swipe.js?p1",
id: 'swipe'
});
$("head").append(css);
$('#swipe').load(function() {
$this.swipe(function(e, dx, dy){
if(dx < 0){
$this.find('.next').click();
} else {
$this.find('.prev').click();
}
});
});
}
,但沒有成功
不錯!現在我只需要處理爲什麼'Modernizer'沒有定義..謝謝! –
@ToniMichelCaubet,使用'Modernizr.touch'而不是'modernizr.touch',我們錯過了一個錯字 – Satpal
是的,我試過......我不明白爲什麼,.js文件可以解決..我猜反正是一個完全不同的問題..謝謝! –