我已將自己的JavaScript包裝在自調用函數中以保持所有內容。 Hoever,我遇到了一個問題,即我的鏈接動態構建,在點擊時返回一個錯誤「Function not defined」。讓我有我的相關代碼:具有動態綁定內容的自調用函數範圍
(function(window, document, $) {
function buildChapterList() {
for(var i=0; i<Object.keys(theChapterCues).length; i++) {
chapterListHTML = "<li><a class='fun-blue' href='javascript:void(0)' onClick=\"skipToChapter('"+ i +"')\">"+ theChapterCues[i]["title"] +"</a></li>";
$(chapterListHTML).appendTo("ul#chapter-list");
$("body").bind("DOMNodeInserted", function() {
$(this).find('#chapter-list li').first().addClass("active");
});
}
}
function skipToChapter(theChapter) {
if (theChapter == 0) {
theVideo.currentTime=0;
} else {
var thisChapterStart = parseInt(cues[theChapter]["chapterStart"]+1);
theVideo.currentTime=thisChapterStart/frameRate;
}
}
}(this, this.document, this.jQuery));
當點擊我收到以下錯誤生成的鏈接之一:
未捕獲的ReferenceError:我失去了對範圍的東西skipToChapter沒有定義
?這是一個有約束力的問題?任何建議將不勝感激。謝謝!
是的,你的功能只存在於你的匿名範圍內。你需要在全局範圍內使用它,或者找到另一種綁定事件的方法(如果你可以提供小提琴,會更容易找到另一種方法) – 2014-10-10 14:16:12