如何从外部文件导入 JavaScript
How to Import JavaScript from external file?
我创建了简单的HTML页面,并在标签中导入了JavaScript。它工作正常(当单击div s 时,使其可见并隐藏)
我想从外部文件导入 JavaScript,我进行了如下更改。然后隐藏的div始终可见,无法隐藏并手动可见
休耕代码有什么问题
工作代码(当 Java 脚本在"head"标签中时)
<html>
<head>
<link rel="stylesheet" type="text/css" href="http://hp-parts.orgfree.com/hpcss.css">
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
$(document).ready(function() {
// Hide all the elements in the DOM that have a class of "box"
$('.box').hide();
// Make sure all the elements with a class of "clickme" are visible and bound
// with a click event to toggle the "box" state
$('.clickme').each(function() {
$(this).show(0).on('click', function(e) {
// This is only needed if your using an anchor to target the "box" elements
e.preventDefault();
// Find the next "box" element in the DOM
$(this).next('.box').slideToggle('fast');
});
});
})
</script>
</head>
<body align="center">
<h1>HP Laptop Parts</h1>
<h2>Compatible with G62 CQ62 G56 CQ56 & MORE </h2>
<a href="#" class="clickme">Keyboard housing Top Cover </a>
<div class="box">
<img src="http://hp-parts.orgfree.com/images/Housing/DSC_0308.jpg">
</div>
<a href="#" class="clickme">Touchpad Buttons Board , Cable & Frame</a>
<div class="box">
<img src="http://hp-parts.orgfree.com/images/Keypad%20Buttons/DSC_0280.jpg">
</div>
</body>
</html>
我将代码更改为波纹管。
<html>
<head>
<link rel="stylesheet" type="text/css" href="http://hp-parts.orgfree.com/hpcss.css">
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="myjs.js"></script>
</head>
<body align="center">
<h1>HP Laptop Parts</h1>
<h2>Compatible with G62 CQ62 G56 CQ56 & MORE </h2>
<a href="#" class="clickme">Keyboard housing Top Cover </a>
<div class="box">
<img src="http://hp-parts.orgfree.com/images/Housing/DSC_0308.jpg">
</div>
<a href="#" class="clickme">Touchpad Buttons Board , Cable & Frame</a>
<div class="box">
<img src="http://hp-parts.orgfree.com/images/Keypad%20Buttons/DSC_0280.jpg">
</div>
</body>
</html>
myjs.js文件
$(document).ready(function() {
// Hide all the elements in the DOM that have a class of "box"
$('.box').hide();
// Make sure all the elements with a class of "clickme" are visible and bound
// with a click event to toggle the "box" state
$('.clickme').each(function() {
$(this).show(0).on('click', function(e) {
// This is only needed if your using an anchor to target the "box" elements
e.preventDefault();
// Find the next "box" element in the DOM
$(this).next('.box').slideToggle('fast');
});
});
})
如果你声明一个文档类型(html 5 doctype)。
http://www.w3schools.com/tags/tag_DOCTYPE.asp
..无需为脚本标记指定类型。
此外,您还需要使用 / 到达路径的基础。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="http://hp-parts.orgfree.com/hpcss.css">
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="/myjs.js"></script>
</head>
还有你的脚本:
$(document).ready(function () {
// Hide all the elements in the DOM that have a class of "box"
$('.box').hide();
// Make sure all the elements with a class of "clickme" are visible and bound
// with a click event to toggle the "box" state
$('.clickme').each(function () {
$(this).show(0).on('click', function (e) {
// This is only needed if your using an anchor to target the "box" elements
e.preventDefault();
// Find the next "box" element in the DOM
$(this).next('.box').slideToggle('fast');
});
});
});
失踪 ; 在最后..
相关文章:
- 导入javascript库一次
- 在try-catch块内的HTML文件中导入JavaScript文件
- 如何从typescript文件导入javascript文件
- 在不干扰HTML代码的情况下,将javascript文件导入javascript文件
- 如何从外部文件导入 JavaScript
- 在 iPython 笔记本中导入 JavaScript 文件以创建自定义小部件
- 从带有 index.js 的“/folder”导入 JavaScript
- 在函数中导入JavaScript.php不起作用
- 将 Python Flask JSON 字典导入 JavaScript 错误
- 导入JavaScript文件-PhoneGap
- 如何将数据从php文件导入javascript变量
- 在Bottle中导入Javascript文件(framework jQuery)
- 如何使用HighStock图表将日期字符串作为UTC对象导入javascript
- 如何使用资产管道在rails中导入javascript
- 如何在CoffeeScript中导入javascript库
- 将.CSV数据导入Javascript,并在字符串包含数据时运行execute
- 使用eval导入javascript中的命名空间
- 如何通过导入javascript中的txt文件来创建列表
- 更好的方式导入JavaScript和CSS文件使用CDN或从本地机器
- 用CSS导入JavaScript文件