假设我有一个如下页面
<html><head></head><body><!--STUFF-->
<script>
if(SomeBooleanVariable) {
$.getScript('js/file.js');
}
</script>
</body></html>
我的 file.js 文件只包含没有包装的原始 jQuery 事件。如下:
$(document).on("eventHere", "classHere", function(e) {
//Stuff
});
$(document).on("eventHere", "classHere", function(e) {
//Stuff
});
这根本行不通。当我将 file.js 的内容直接包含到 HTML 中时,它可以正常工作,但是 JS 似乎没有正确包含。我试过把“alert(3);” 在 file.js 的顶部,但它不会触发。我尝试了以下方法:
$("head").append("<script src=\"js/file.js\" />");
$(document).append("<script src=\"js/file.js\" />");
-and-
document.write("<script src=\"js/file.js\" />");