ランダムな動物の名前を表示する
以下のボタンを押すとランダムな動物の名前を表示します。
以下は、ユーザーの要望に基づいて作成されたJavaScriptコードです。フォントカラーやテキストサイズをランダムに選択するようになっています。また、アラートを出力するようにして、ジョークを追加しました。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ランダムな動物の名前を表示</title>
<style>
#random-animal {
font-size: 16px;
color: black;
}
.elephant {
font-size: 32px;
color: gray;
}
.lion {
font-size: 24px;
color: goldenrod;
}
.toucan {
font-size: 20px;
color: #008000;
}
</style>
</head>
<body>
<h1>ランダムな動物の名前を表示する</h1>
<p>以下のボタンを押すとランダムな動物の名前を表示します。</p>
<button onclick="displayRandomAnimal()">クリックしてランダムな動物を表示</button>
<p id="random-animal"></p>
<script type="text/javascript">
const animalList = [
{ name: "象", font: "elephant" },
{ name: "ライオン", font: "lion" },
{ name: "オウムヅカミ", font: "toucan" },
{ name: "猿", font: "" },
{ name: "ジャガー", font: "" },
{ name: "トナカイ", font: "" },
{ name: "クマ", font: "" },
{ name: "コアラ", font: "" },
{ name: "カワウソ", font: "" },
{ name: "イヌワシ", font: "" }
];
function displayRandomAnimal() {
const randomIndex = Math.floor(Math.random() * animalList.length);
const animal = animalList[randomIndex];
const animalName = animal.name;
const animalFont = animal.font;
const randomColor = Math.floor(Math.random()*16777215).toString(16);
const output = document.getElementById("random-animal");
output.textContent = animalName;
output.className = animalFont;
output.style.color = "#" + randomColor;
alert("ランダムな動物が表示されました!\n「" + animalName + "」です。");
}
</script>
</body>
</html>
```
このコードでは、ランダムな動物を表示するたびに、ランダムなフォントカラーとテキストサイズを選択しています。また、選ばれた動物の種類によって、フォントの種類も変更するようにしています。最後に、ランダムな動物が選ばれた際にアラートを表示し、ジョークを追加しています。