繁体   English   中英

如何将JavaScript添加到HTML页面?

[英]How to add javascript to HTML Page?

我是网络开发的新手,我想在我的简单html页面中添加以下功能,但如果我按下“click me”就不会发生任何事情。 描述部分不隐藏和显示

这是我试过的代码

我添加了以下代码。 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库。

始终检查控制台是否有错误。

暂无
暂无

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

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