[英]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
文件时没有得到任何结果。
index.js
name referenced in the .html
file is the same.html
文件中引用的index.js
名称是否相同src=index.js
jQuery library through Google to the bottom before </body>
src=index.js
jQuery 库添加到底部之前</body>
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.