我想濃縮我的代碼,因爲我有很多重複編碼發生。我將需要多次應用此相同的示例。我想創建一個for循環,但我的變量也需要增加。現在我有我的變量增加,但我無法實現我的細胞數據到變量。我想我是雙重分配變種h。我無法弄清楚如何解決這個問題。感謝您的幫助。申請循環變量
For循環
for (var j = 2; j<15; j++){eval("var polebrea" +j);
var h = ("polebrea" +j)
}
h = document.getElementById("part1Table").rows[10].cells[2].innerHTML;
代碼試圖實施
polebrea2 = document.getElementById("part1Table").rows[10].cells[2].innerHTML;
polebrea3 = document.getElementById("part1Table").rows[10].cells[3].innerHTML;
polebrea4 = document.getElementById("part1Table").rows[10].cells[4].innerHTML;
polebrea5 = document.getElementById("part1Table").rows[10].cells[5].innerHTML;
(續15)
插入可變
<script>document.write(polebrea2)</script>
不要使用'eval'。創建一個可以索引的數組。 – 2014-09-12 16:32:09
使用一個對象來存儲它們,而不是爲每個對象創建一個新變量。 'object [「polebrea」+ j]' – GillesC 2014-09-12 16:32:13
動態命名變量是某處設計不好的標誌。使用可以容納某些東西的數據結構,如對象或數組。 – 2014-09-12 16:33:17