[英]Javascript Game for Kids
我正在嘗試用javascript制作游戲。 我的代碼中仍然有很多問題。 代碼應該有2個按鈕; 一個用於洗牌圖像骰子和一個按鈕,以檢查孩子是否寫了正確的答案。
這是我的代碼。
我不知道為什么當我把document.getElementbyID(“test”)的來源放在什么時候什么都沒有,因為我希望每次點擊開始隨機圖像都被選中。
我很感激任何幫助,因為我仍然是javascript的初學者。
<head>
<script type="text/javascript">
function startf(){
var images = [];
index = 0;
images[0] = "<img src='1.png' length=70px width=75px>";
images[1] = "<img src='2.png' length=70px width=75px>";
images[2] = "<img src='3.png' length=70px width=75px>";
images[3] = "<img src='4.png' length=70px width=75px>";
index = Math.floor(Math.random() * images.length);
take(index);
function take(ind)
{
return document.getElementbyId("ind")="What should i put here";
}
}
function check(){
var ch=getElementbyId("answer").value;
if (ch=index+1)
{
document.getElementbyId.innerHTML="TRUE";
}
else
{
document.getElementbyId.innerHTML="False";
}
}
</script><br>
</head>
<img id="test" src="" alt="..." length="75px" width="75px" />
<body>
<input type="button" value="start" onclick="startf()">
<input id="answer" type="text" name="checkvalue" value="Enter Value" onclick="check()">
<div id="fa">
</div>
<input type="button" value=chek onclick="check()">
</body>
1-放置和結束每條指令 - > ;
2-不要直接使用document.getElementById,至少會出現一個錯誤,並且你不想這樣做。
function _e(id) {
return document.getElementById(id);
}
3-始終在IF-ELSE塊周圍放置括號和(...):
if (...) {
//....
} else {
//....
}
4-每個標記屬性的值都應該是“”,例如:
<input type="button" value="start" onclick="check()" />
5-您只能在陣列中放置圖像路徑,因為它似乎需要在#test圖像中更新。
它是document.getElementById
檢查外殼。 您的檢查功能錯誤...您無法為document.getElementById函數賦值。 你的if也錯了。 你知道任何JavaScript嗎?
我猜你想要的東西可能是這樣的。 您似乎只是嘗試使用document.getElementById(id) = something
來添加或替換元素,但這不是它的工作方式。 相反,要將圖像更改為另一個文件,您需要更改其src
屬性。 (還有其他方法,但這可能是最簡單的。)
// Declare the variable called number here so it can be accessed
// by all of the following functions.
var number;
// a simple function to save typing document.getElementById all the time
function _e(id) {
return document.getElementById(id);
}
function startf() {
// Pick a number between 1 and 4
number = Math.floor(Math.random() * 4 + 1);
// Set the image with id 'test' to have the source 1.png, 2.png etc.
_e('test').src = number + '.png';
}
function check() {
// note that == is used for comparison, = is used for assignment
if (_e('answer').value == number) {
_e('fa').innerHTML = "TRUE";
}
else {
_e('fa').innerHTML = "False";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.