如何在 html5 中包含 javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30910510/
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
How to include javascript in html5
提问by RM GoodMan
I can not connect an external JavaScript file to my html page. When I put the script in the page with the tag it all works but when I insert it in an external file is not working, what is wrong?
我无法将外部 JavaScript 文件连接到我的 html 页面。当我将脚本放在带有标签的页面中时,它一切正常,但是当我将它插入到外部文件中时却不起作用,有什么问题?
<!DOCTYPE!>
<head>
<link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
<!-- JQuery da Google -->
<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<!---------------------->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document</title>
<!-- CSS -->
<link href="style.css" rel="stylesheet" type="text/css" />
<!-- JS-->
<script src="js/function.js" type="text/javascript"></script>
</head>
<body>
<footer>
<img class="info" src="img/newsletter.png" width="32" height="32" alt="info" />
</footer>
<div id="info">
<ul class="infomenu">
<li class="newsletter">NEWSLETTER</li>
<li>PRIVACY</li>
<li>CONTACT</li>
<li>FOLLOW US</li>
</ul>
</div>
</body>
</html>
Javascript
Javascript
//Jquery Info
$(document).ready(function(){
$(".info").hover(function(){
$("#info").fadeIn("slow");
});
$(".close").click(function(){
$("#info").fadeOut("slow");
});
});
回答by Cagatay Ulubay
You really messed up your html code, try googling for the HTML(5) basics, first of you should learn the basic construction of it like following:
你真的搞砸了你的 html 代码,尝试谷歌搜索 HTML(5) 基础知识,首先你应该学习它的基本结构,如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8">
<title>Welcome</title>
<link type="text/css" href="styles/default.css">
</head>
<body>
<!-- HTML Content -->
<script type="text/javascript" src=".."></script>
<script>
// Javascript inside this file
</script>
</body>
</html>
The link
- and script
part is not necessary, but you mostly will need it so I put it in the right order in.
Try putting script
-Tags over the closing </body>
-Tag, this will prevent that the page is loading endless for the Javascript file, before the page is actually loaded.
的link
-和script
部分是没有必要的,但你大多会需要它,所以我把它放在正确的顺序尝试把。script
标签都有效在关闭</body>
-Tag,这将阻止该页面加载无尽的JavaScript文件之前,页面实际加载。
This way the external Javascript should work, also if you working localy, you should use a Webserver software like XAMPP. If you use XAMPP, after installing it, you have to start the Apache Service
and then you work inside (if you didn't changed the path) the C:\xampp\htdocs
folder. If you create a folder inside it called testing
and place your index.php
inside it, you just can type following in the browser http://localhost/testing
and it will search for a index. html or php file and parse it.
这样外部 Javascript 应该可以工作,如果你在本地工作,你应该使用像 XAMPP 这样的网络服务器软件。如果您使用 XAMPP,则在安装后,您必须启动Apache Service
该C:\xampp\htdocs
文件夹,然后在该文件夹中工作(如果您没有更改路径)。如果您创建一个文件夹里面叫testing
,把你index.php
里面,你就可以输入浏览器下面http://localhost/testing
,它会搜索索引。html 或 php 文件并解析它。
If you just double click the file, you mostly will end up with security issues, which will prevent your code will work like you intended to do. You know that you double clicked a file if it starts like file://
and not http://
.
如果您只是双击该文件,您通常会遇到安全问题,这将阻止您的代码按预期工作。你知道你双击了一个文件,如果它开始喜欢file://
和不http://
。
But like I said, google for tutorials from the scratch. It takes time, but you can't do it without taking the time. Trust me, I do this for over 7 Years now and I am online nearly everyday and learning, learning, reading, testing, coding, learning, reading, testing and I STILL think that this is less than 5% of knowledge what I could learn.. never think you are at the end or near to it.. you never are, there are always things to learn and if you keep in thought that you are near the end, you will stop improving and never become good.
但就像我说的,从头开始谷歌搜索教程。这需要时间,但你不能不花时间去做。相信我,我这样做已经超过 7 年了,我几乎每天都在网上学习、学习、阅读、测试、编码、学习、阅读、测试,我仍然认为这还不到我能学到的知识的 5% ..永远不要认为你已经到了终点或接近终点了..你永远不会,总有一些东西要学习,如果你一直认为自己已经接近终点,你将停止进步,永远不会变得更好。
回答by Casper Round
<script>
$(document).ready(function(){
$(".info").hover(function(){
$("#info").fadeIn("slow");
});
$(".close").click(function(){
$("#info").fadeOut("slow");
});
});
</script>