gpt4 book ai didi

javascript - 单击按钮时运行函数

转载 作者:行者123 更新时间:2023-11-28 17:36:49 25 4
gpt4 key购买 nike

我有一个非常简单的问题,但我无法理解。我已经创建了函数,并希望在单击按钮后立即运行该函数。应该很简单吧?将 onclick 事件添加到我的按钮。但我就是无法让它运行。我错过了什么吗?

这是我的标记:

<!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">
<link rel="stylesheet" href="css/styles.css">
<title>Get To Know Each Other...</title>
</head>
<body>
<div class="wrapper">
<h1 id="question"></h1>
<button onclick="showAnswer()">Next question</button>
</div>
<script src="js/script.js"></script>
</body>
</html>

这是我的 JS:

var question = [
'If you didn’t have to sleep, what would you do with the extra time?',
'What’s your favorite piece of clothing you own / owned?',
'What hobby would you get into if time and money weren’t an issue?',
'How often do you play sports?',
'What job would you be terrible at?',
'When was the last time you climbed a tree?',
'What songs have you completely memorized?',
'What game or movie universe would you most like to live in?',
'What takes up too much of your time?',
'What’s your favorite genre of book or movie?',
'What’s the best single day on the calendar?',
'How do you relax after a hard day of work?',
'What’s the farthest you’ve ever been from home?',
'What is the most annoying question that people ask you?',
'Would you rather go hang gliding or whitewater rafting?',
'What’s your dream car?'
]

var rand = Math.floor(Math.random() * question.length);

function showAnswer(){
document.getElementById("question").innerHTML = question[rand];
}

showAnswer();

这是我的 fiddle :https://jsfiddle.net/5cabtx79/1/

最佳答案

var rand = Math.floor(Math.random() * Question.length); 放在函数内。在函数外部,其中的值只会被设置一次。但根据要求,每次单击按钮时都需要更改该值

var question = [
'If you didn’t have to sleep, what would you do with the extra time?',
'What’s your favorite piece of clothing you own / owned?',
'What hobby would you get into if time and money weren’t an issue?',
'How often do you play sports?',
'What job would you be terrible at?',
'When was the last time you climbed a tree?',
'What songs have you completely memorized?',
'What game or movie universe would you most like to live in?',
'What takes up too much of your time?',
'What’s your favorite genre of book or movie?',
'What’s the best single day on the calendar?',
'How do you relax after a hard day of work?',
'What’s the farthest you’ve ever been from home?',
'What is the most annoying question that people ask you?',
'Would you rather go hang gliding or whitewater rafting?',
'What’s your dream car?'
]



function showAnswer() {
var rand = Math.floor(Math.random() * question.length);
document.getElementById("question").innerHTML = question[rand];
}

showAnswer();
<div class="wrapper">
<h1 id="question"></h1>
<button onclick="showAnswer()">Next question</button>
</div>

关于javascript - 单击按钮时运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48970593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com