简体   繁体   English

如何在HTML5中包含JavaScript

[英]How to include javascript in html5

I can not connect an external JavaScript file to my html page. 我无法将外部JavaScript文件连接到html页面。 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? 当我将脚本放入带有标签的页面时,所有脚本都可以正常工作,但是当我将其插入外部文件中却无法正常工作时,怎么了?

<!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");
    });

});

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. linkscript部分不是必需的,但您通常会需要它,因此我将其按正确的顺序放置。尝试将script -Tags放在结束</body> -Tag上,这样可以防止页面无休止地加载在页面实际加载之前用于Javascript文件。

This way the external Javascript should work, also if you working localy, you should use a Webserver software like XAMPP. 这样,外部Javascript应该可以工作,如果在本地工作,也应使用XAMPP之类的Web服务器软件。 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. 如果使用XAMPP,则在安装XAMPP之后,必须启动Apache Service ,然后在C:\\xampp\\htdocs文件夹中进行内部操作(如果未更改路径)。 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. 如果在其中创建一个名为testing的文件夹并将index.php放在其中,则只需在浏览器http://localhost/testing键入以下内容,它将搜索索引。 html or php file and parse it. 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%。 ..永远不要以为自己是末日或终极将..从来没有,总是有很多东西要学习,如果您一直以为自己就在末日,您将停止进步,永远也不会变得好。

<script> $(document).ready(function(){ $(".info").hover(function(){ $("#info").fadeIn("slow"); }); $(".close").click(function(){ $("#info").fadeOut("slow"); }); }); </script>

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

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