“开始”按钮不会在VS代码上发出alert(),但可在Codepen上使用

时间:2019-05-07 01:37:36

标签: javascript html twitter-bootstrap

我试图仅使我的“开始”按钮在单击时向我发出警报或console.log。但是,当我尝试对其进行测试时,这仅适用于Codepen,而不适用于构建此代码的VS Code。我不知道这是否与Bootstrap 4有关。

我尝试过更新引导CDN并移动引导4个脚本

JavaScript

var startButton = document.getElementById("start");

function alertButton(){
    alert("button was clicked");
    console.log("button was clicked");
}

startButton.addEventListener('click', alertButton);

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Pomodoro App</title>
    <link rel="stylesheet" href="styles.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css?family=Gloria+Hallelujah" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">


</head>
<body>

        <div class="header">   

                <h1 id="pom-header">Pomodoro App</h1>
        </div>
    <div class="container">
        <div class="buttons">
                <button id ="start" type="button" >Start</button>

        </div>
    </div>
    <div class="container">
        <span id="timer">25</span>
    </div>




    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>

2 个答案:

答案 0 :(得分:3)

您似乎没有将JS文件导入HTML。在导入jQuery的下方,放下<script src="/path/to/jsfile.js"></script>

答案 1 :(得分:0)

在这里,我做了一些工作才能使您的点击事件正常运行。该脚本必须位于您要向其添加click事件的元素下方。复制并粘贴:

<!DOCTYPE html>
<html lang="en">
<head>



    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Pomodoro App</title>
    <link rel="stylesheet" href="styles.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css?family=Gloria+Hallelujah" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">


</head>
<body>

        <div class="header">   

                <h1 id="pom-header">Pomodoro App</h1>
        </div>
    <div class="container">
        <div class="buttons">
                <button id ="start" type="button" >Start</button>

        </div>
    </div>
    <div class="container">
        <span id="timer">25</span>
    </div>



<script>
    var startButton = document.getElementById("start");

    function alertButton(){
        alert("button was clicked");
        console.log("button was clicked");
    }

    startButton.addEventListener('click', alertButton);
</script>
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>