我正在为Old McDonald歌曲编写代码。我有3只动物作为用户的选择。根据他们的输入,我编写了代码,以便歌曲以动物声音作为文本输出。我还想为onclick
事件添加一个声音,但每个动物都会改变它。这可能吗?
以下是目前的代码:
<!DOCTYPE html>
<script>
function songFunction() {
var output;
var animal = prompt("Choose an animal. Cow, chicken, or sheep?", "Enter
an animal.");
if (animal == null) {
output = "You don't want to sing along?";
}
else if (animal.toUpperCase() == "COW") {
output = "With a moo moo here, and a moo moo there. Here a moo,
there a moo, everywhere a moo moo!";
}
else if (animal.toUpperCase() == "CHICKEN") {
output = "With a cluck cluck here, and a cluck cluck there. Here a
cluck, there a cluck, everywhere a cluck cluck!";
}
else if (animal.toUpperCase() == "SHEEP") {
output = "With a baa baa here, and a baa baa there. Here a baa,
there a baa, everywhere a baa baa!"
}
else {
output = "This farm only has 3 kinds of animals. Please try again."
}
document.getElementById("message").innerHTML = output;
}
</script>
<html>
<head>
<title>Old McDonald's Farm</title>
</head>
<body>
<p>Old McDonald had a farm. And, on his farm he had a....</p>
<button onclick="songFunction()">What comes next?</button>
<p id="message"></p>
</body>
</html>