简体   繁体   English

为什么当我按开始时我的 console.log 消息不显示?

[英]Why doesn't my console.log message show when I press Start?

I'm learning to use event listeners, I wanted to log pressing the "Start" button to the console to make sure the button works but I'm not getting any results when I test the .html file in devtools.我正在学习使用事件侦听器,我想记录按下控制台的“开始”按钮以确保该按钮有效,但是当我在 devtools 中测试.html文件时没有得到任何结果。

  • I've verified the index.js name referenced in the .html file is the same我已验证.html文件中引用的index.js名称是否相同
  • I've tried the source code in the header我已经尝试过 header 中的源代码
  • I've added the src=index.js jQuery library through Google to the bottom before </body>我已经通过谷歌将src=index.js jQuery 库添加到底部之前</body>
  • I've copied the code to repl.it to try a different environment我已将代码复制到repl.it以尝试不同的环境

JAVASCRIPT: JAVASCRIPT:

function startQuiz() {
    $('#startButton').click(function(event){
        console.log("Keep Going");
    });
}

HTML: HTML:

<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Herbal Medicine: Adaptogens</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <link rel="questions" href="store.html">
</head>
<body>
    <div class="container">
        <p>Intro to Herbal Adaptogens explaining what they are</p>
        <button id="startButton">Start</button>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="index.js"></script>
</body>
</html>

I expect the to see "Keep Going" in the console when I press "start" but instead nothing happens at all.当我按“开始”时,我希望在控制台中看到“继续”,但什么都没有发生。

You have the JQuery click() function inside the startQuiz() function.您在 startQuiz click() function 中有 JQuery startQuiz() function。 So it won't work unless the outer function ( startQuiz() ) is run first.因此,除非先运行外部 function ( startQuiz() ),否则它将无法工作。 You should put it inside a ready function so that it loads once the page is 'ready'.您应该将它放在准备好的 function 中,以便在页面“准备好”后加载。

Change your javascript to look like this:将您的 javascript 更改为如下所示:

$(document).ready(function() {
    $('#startButton').click(function(event) {
        console.log("Keep Going");
    });
});

You should put it inside a Jquery click function.你应该把它放在一个 Jquery 点击 function。 This way it will fire when the document loads.这样,它会在文档加载时触发。

$(document).ready(function() {
    $('#startButton').click(function(event) {
        console.log("Keep Going");
    }
});

No need to go with jQuery, you can just use js events.不需要 go 和 jQuery,你可以只使用 js 事件。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Log Window</title>
</head>
<body>
  <script>
  function logMe() {console.log('clicked');}
  </script>
  <button onclick="logMe()">logMe</button>
</body>
</html>

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

相关问题 获取后 console.log 不显示消息 - console.log doesn't show a message after fetch 如果方法抛出异常,Node.js:console.log消息不显示...为什么? - Node.js: console.log message doesn't show up if method throws exception… why? 当打印整个对象时,为什么console.log不显示PROTOTYPES在javascript中添加的属性值? - why console.log doesn't show values of properties added by PROTOTYPES in javascript when whole object is printed? 如果我复制并粘贴它,为什么 console.log() 没有任何内容? - Why doesn't it console.log() anything if I copy and paste it? 为什么我的 React 组件不会在 foreach 中呈现我想要的内容,但会使用 console.log 记录数组? - Why doesn't my React component renders what I want in a foreach, but will console.log the array? console.log 不会将消息打印到 firebug 控制台? - console.log doesn't print the message to the firebug console? 为什么 console.log 不使用 VS Code 在控制台中显示结果? - Why doesn't console.log show the result in the console using VS Code? 当我按下键盘上的一个键时如何使用 console.log? - How to console.log when I press a key in the keyboard? console.log 不显示由类创建的函数 - console.log doesn't show functions created by class SerializeObject和SerializeArray没有显示在console.log上 - SerializeObject and SerializeArray doesn't show on console.log
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM