从HTML链接时,javascript文件不起作用 [英] javascript file not working when linked from HTML

查看:82
本文介绍了从HTML链接时,javascript文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我觉得(和希望)这很简单。我是新的javascript,我正在努力让这个工作。当我从我的html链接到我的外部.js文件时,它不起作用。但是,当脚本代码直接输入我的HTML时,它可以工作。

so I feel(and hope) this is pretty simple. I am new to javascript and am trying to get this working. When I link to my external .js file from my html it does not function. However, when entering the script code directly into my HTML it DOES work.

这是js文件:

$(document).ready(function(){
  $("#flip").click(function(){
    $("#panel").slideToggle("slow");
  });
});

这是index.html文件:

This is the index.html file:

<!DOCTYPE html>
<html>
<head>
    <title>slidepanel test</title>
<script type="text/javascript" src="script.js"></script>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>

<body>


<div id="flip">Click to slide the panel down or up</div>
<div id="panel">Hello world!</div>

</body>
</html>

这是CSS:

#panel,#flip
{
padding:5px;
text-align:center;
background-color:#e5eecc;
border:solid 1px #c3c3c3;
}
#panel
{
padding:50px;
display:none;
}


推荐答案

您正在使用jQuery,看起来你似乎没有包括它。将其添加到您的HEAD元素

You are using jQuery, but it doesn't seem like you have included it. Add this to your HEAD element

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

这篇关于从HTML链接时,javascript文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆