javascript 将 jquery 代码移动到外部文件不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25082539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Moving jquery code to external file not working
提问by Khalil
I am trying to move jquery code from internal html file to external file and link it in the head of html file, the site is working inside html but when I move the code to external file the link is not working.
我正在尝试将 jquery 代码从内部 html 文件移动到外部文件并将其链接到 html 文件的头部,该站点在 html 内部工作,但是当我将代码移动到外部文件时,链接不起作用。
HTML head code:
HTML头部代码:
<head>
<link rel="stylesheet" href="/newsite/style.css" title="new" type="text/css" media="screen" charset="utf-8">
<script type="text/javascript" src="./newsite/js/scripting.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
回答by Spencer Wieczorek
The problem is here:
问题在这里:
<script type="text/javascript" src="./newsite/js/scripting.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
You are trying to use jQuery before you loaded the jQuery library. You need to switch these around like so:
您在加载 jQuery 库之前尝试使用 jQuery。您需要像这样切换这些:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="./newsite/js/scripting.js"></script>
Although I don't recommend getting the latest jQuery, you never know what may be deprecated in the future.
尽管我不建议使用最新的 jQuery,但您永远不知道将来可能会弃用什么。
If that does not fix the problem make sure you directories are the correct ones. Check the console on your browser for error messages.
如果这不能解决问题,请确保您的目录是正确的。检查浏览器上的控制台是否有错误消息。
回答by Chrissi
If you use Google Chrome, press F12 to open Dev Tools and click on the "console" tab. Then refresh the page. You should see some sort of error as to why your script is not working.
如果您使用谷歌浏览器,请按 F12 打开开发工具并单击“控制台”选项卡。然后刷新页面。您应该会看到某种错误,说明您的脚本为何不起作用。
If there is no error, click the "network" tab and look for your JavaScript file. Any files loaded into the page should show up here. If if isn't there, it wasn't loaded, which means you haven't properly added the script tag to your HTML.
如果没有错误,请单击“网络”选项卡并查找您的 JavaScript 文件。加载到页面中的任何文件都应显示在此处。如果不存在,则表示未加载,这意味着您没有将脚本标记正确添加到 HTML 中。
回答by isick
If you're running this from a local HTML file, I'm pretty confident your problem is the file isn't being included.
如果您从本地 HTML 文件运行此文件,我非常确信您的问题是该文件未包含在内。
The code to include it should probablybe
该代码包括它应该可能是
<script type="text/javascript" src="newsite/js/scripting.js"></script>
or
或者
<script type="text/javascript" src="./newsite/js/scripting.js"></script>
But just starting with a backslash means the current domain's root. Which, if you're running the file locally, will be your C drive (probably).
但仅以反斜杠开头表示当前域的根。其中,如果您在本地运行该文件,则将是您的 C 驱动器(可能)。
I say probably because you haven't given much to go on.
我说可能是因为你没有付出太多的努力。
回答by Tommy Davies
Place a console.log();at the bottom of your script, load your page with the developer tools console open, if you can see your log your script is running, there may be another external JavaScript file or an internal script that is interfering with your script which is stopping it from working, check to see what scripts you have loading in the of your page, have the script that's not working loaded at the bottom of the page so the script is already loaded when the page loads.
放置一个console.log(); 在脚本的底部,在打开开发者工具控制台的情况下加载页面,如果您可以看到脚本正在运行的日志,则可能有另一个外部 JavaScript 文件或内部脚本干扰了您的脚本,从而阻止了它工作,检查您在页面底部加载了哪些脚本,在页面底部加载不起作用的脚本,以便在页面加载时已经加载了脚本。
Your source directory could be wrong also, when you load your page open up the developer tools and select 'console' to check if you have an error that the file could not be found.
您的源目录也可能是错误的,当您加载页面时,打开开发人员工具并选择“控制台”以检查您是否有找不到文件的错误。
*edit
*编辑
The console is showing: Failed to load resource: the server responded with a status of 404 (Not Found).
控制台显示:无法加载资源:服务器响应状态为 404(未找到)。
This is the link:http://khaleha3rbi.com/newsite/newsite/js/scripting.js
这是链接:http: //khaleha3rbi.com/newsite/newsite/js/scripting.js
You need to removeone of the "newsite" folders in that directory link.
您需要删除该目录链接中的“newsite”文件夹之一。
回答by Khalil
I have solved it, the solution was to switch between to lines in the <head>
tag.
我已经解决了,解决方案是在<head>
标签中的行之间切换。
it was like that:
事情是这样的:
<script type="text/javascript" src="/newsite/js/scripting.js"></script>
<script type="text/javascript" src="/newsite/js/scripting.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
I make it:
<script src="http://code.jquery.com/jquery-latest.js"></script>
我做到了:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="/newsite/js/scripting.js"></script>
<script type="text/javascript" src="/newsite/js/scripting.js"></script>
and it works.
它有效。
Thank you all for help
谢谢大家的帮助