简体   繁体   English

从HTML调用JavaScript脚本

[英]Calling javascript script from html

I have a simple javascript file like so: 我有一个简单的javascript文件,如下所示:

$(document).ready(function () {
    alert("my controller");
});

I have an HTML file like so: 我有这样的HTML文件:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript" src="/js/generateLineupController.js"></script>
</body>
</html>

for the love of all things holy why in the world would the alert not show? 对于万物的热爱圣洁,为什么在世界上不会显示警报? I get a 200 on GET for the javascript file, so it's loading. 我在javascript文件的GET上得到200,因此正在加载。

Several problems here. 这里有几个问题。

  1. You're trying to load the script twice for some reason. 由于某种原因,您试图两次加载脚本。 Don't do that. 不要那样做 Load it in <head> , or at the end of <body> , but not both. 将其加载到<head> <body>的末尾,但不能同时加载两者。

  2. You're trying to use jQuery syntax ( $(...) ), but you haven't loaded the jQuery library. 您正在尝试使用jQuery语法( $(...) ),但尚未加载jQuery库。 You'll need that. 您将需要它。

The $(document).ready(...) indicates that you are trying to use jQuery, but you aren't loading jQuery in your page. $(document).ready(...)表示您正在尝试使用jQuery,但未在页面中加载jQuery。 Check your console log; 检查您的控制台日志; you will see the errors there. 您将在此处看到错误。

Also, there's no need to load the script twice with two <script> tags; 另外,也不需要使用两个<script>标签将脚本加载两次。 just load it once. 只需加载一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM