Javascript 外部Js文件无法运行Jquery

Javascript 外部Js文件无法运行Jquery,javascript,jquery,html,external,Javascript,Jquery,Html,External,我的外部js文件example.js: alert("External js file has just been loaded !"); $("#start").click(function (e) { alert("Starting !"); }); 我的HTML文件已被删除。 当我加载页面时,它会提醒外部js文件刚刚加载!,这意味着js文件已成功加载 但是,当我单击“开始”按钮时,它不会提醒正在启动 当我将example.js文件的内容插入html文件时,它会提醒启动!:

我的外部js文件example.js:

alert("External js file has just been loaded !");

$("#start").click(function (e) {
    alert("Starting !");  
});
我的HTML文件已被删除。 当我加载页面时,它会提醒外部js文件刚刚加载!,这意味着js文件已成功加载

但是,当我单击“开始”按钮时,它不会提醒正在启动

当我将example.js文件的内容插入html文件时,它会提醒启动!:


如何解决外部js文件的此类问题?

之所以发生这种情况,是因为您尚未在document.ready函数中编写click事件。应该在DOM就绪时编写事件

$(document).ready(function(){
  $("#start").click(function (e) {
    alert("Starting !");  
  });
});
您可以在此处找到更多详细信息:

尝试使用$.getScript在.ready加载example.js


我刚试过这个,一切似乎都很好

$document.readyfunction{ $world-button.clickfunction{ 世界你好; }; }; 演示 你好,世界
确保在example.js之前插入了jquery脚本

例如:

<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="example.js"></script>

如果出现任何错误,请检查控制台。

是否在页面上存在start元素之前加载example.js文件?您应该在页面末尾加载js文件,或者将单击处理程序放入文档就绪调用中。@j08691如何在页面末尾加载js文件?移到前面。请注意,我刚才嵌入了JQuery库。
$(function() {
  $.getScript("example.js")
})
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="example.js"></script>