我已将自己的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