如何将javascript添加到HTML页面
How to add javascript to HTML Page?
我是Web开发的新手,我想在我的简单html页面中添加以下功能,但是如果我按"单击我"不会发生任何事情。 描述部分不隐藏和显示
这是我尝试过的代码
我添加了这些代码,如下所示。CSS运行良好。但是JavaScript不起作用。如何解决此问题
<html>
<head>
<title>TEST</title>
<link rel="stylesheet" type="text/css" href="hpcss.css">
<script>
// 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">
<a href="#" class="clickme">Click Me</a>
<div class="box">
First Description
</div>
<a href="#" class="clickme">Click Me</a>
<div class="box">
Second Description
</div>
</body>
</html>
你需要导入 jQuery,并且只有在 jQuery 加载后才运行代码
HTML:
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
JS:
$(function() {
// your code here
})
小提琴正在工作,因为jsfiddle
在 DOM 加载时自动运行,并为您插入jQuery
。
以下是jsfiddle
(幕后)的实际来源:
<script type='text/javascript'>//<![CDATA[
$(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>
你应该做这样的事情:
$(document).ready(function() {
// add your code here
})
您在完成加载 HTML 之前运行 JavaScript -->错误
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
在脚本之前嵌入此内容。
尝试将 js 代码包装到
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(function() {
// your code will be here
});
</script>
这意味着您的代码将在加载页面元素后执行。我还添加了jquery包含。
在创建 DOM 之前附加事件。当您呼叫时
// 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');
});
});
尚未创建具有 clickme
类的元素。
您可以通过将脚本标签放在 </body>
标签之前来修复它,但通过将 javascript 包装在自执行函数中,如下所示:
$(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');
});
});
});
此外,您不包括jQuery库。
始终检查控制台是否存在错误。
相关文章:
- 使用javascript将动态表从一个html页面打印到另一个html页
- 在chrome.tabs.onCreated之后加载HTML页面
- 如何在vs2002中调试html页面
- 使用angular重定向到html页面
- Html页面上的多个Base64图像和平滑加载
- 无法将数据从firebase获取到我的html页面
- 如何在chrome扩展中重定向到html页面
- 如何将值传递到上一个html页面
- AngularJS-需要在index.html页面中访问来自服务的数据
- 如何使用Node/Express显示有关特定错误的自定义html页面
- 如何在 html 页面中调用 jquery
- 加载存储在IndexedDB中的HTML页面
- 同一HTML页面中的两个不同版本的JQuery
- HTML页面如何提取通过表单传递的参数
- 在HTML页面上将URL解析为可读的json格式
- 使用jQuery插入HTML页面的第一个项目缺少结束标记
- 在HTML页面上显示node.js服务器中的数据
- 加载页面时更改html页面的位置
- Ajax调用返回当前html页面,而不是请求的文件
- css是从远程node.js添加的,但不适用于html页面