我遇到的問題是每當我嘗試循環newRow()
函數時,我的網頁都無法執行任何操作。我嘗試了onload,但它仍然不起作用。我也用於循環;也沒有工作。該表仍然是空的,我可以添加另一行的唯一方法是單擊「新建行」按鈕。Javascript循環中的函數調用失敗
這裏是my-webpage.html
:
<!doctype html>
<html>
<head>
<title>
My First Webpage
</title>
</head>
<body>
<script type="text/javascript" src="tables.js">
</script>
<h2>Game Development Research</h2>
<h6>Compiled by Reuben Unicruz</h6>
<hr>
<h3><u><em>Games Analysis</em></u></h3>
<h4>Halo 4</h4>
<iframe width="560" height="315" src="https://www.youtube.com/embed/HMOWXHlxUM0" frameborder="0" allowfullscreen></iframe>
<br/>
<hr>
<h3><u><em>Game Mechanics</em></u></h3>
<h4>First Person Shooters</h4>
<iframe width="560" height="315" src="https://www.youtube.com/embed/13iMO9s-q0c" frameborder="0" allowfullscreen></iframe>
<br/>
<h4>Jumping in 2D</h4>
<iframe width="560" height="315" src="https://www.youtube.com/embed/yuRRPT-Isp4" frameborder="0" allowfullscreen></iframe>
<hr>
<h3>Game Ratings</h3>
<form>
<table id="GameRating">
<thead>
<th>Title</th><th>Genre</th><th>Score</th>
</thead>
</table>
<input type="button" value="New Row" onClick="newRow()"></input>
<input type="button" value="Delete Last Row" onClick="deleteLastRow()"></input>
</form>
</body>
</html>
這裏是tables.js
文件:
function newRow()
{
var table = document.getElementById("GameRating");
var row = table.insertRow(table.rows.length);
var title = row.insertCell(0);
var genre = row.insertCell(1);
var score = row.insertCell(2);
title.innerHTML = "<input type=\"text\"></input>";
genre.innerHTML = "<select><option>Platformer</option><option>FPS</option><option>TPS</option></select>";
score.innerHTML = "<input type=\"text\"></input>";
}
function deleteLastRow()
{
var table = document.getElementById("GameRating");
table.deleteRow(table.rows.length - 1);
}
function printHello()
{
document.write("HELLO!!!<br/>")
}
for (i = 0; i < 3; i++)
{
newRow();
}
爲什麼它不工作? 順便說一句trincot,你鏈接到的答案是不同於我的情況,因爲如果我沒有調用newRow()
函數並使用document.write
來代替,for循環將工作,並且document.write
函數可以工作。另外,腳本已經正確安排。
請添加HTML剩下的部分,以及,看看這裏:[MCVE]。 –
但循環在哪裏? – Teemu
你的元素與'GameRating' ID在哪裏? –