我用按鈕填充我的數組(帶有document.create(「input」))。我想將它們打印到屏幕上,但我無法獲取元素mainArr [i] [j]。當我console.log()它,打印未定義但超出範圍它給我所期望的。我曾經嘗試這樣做:如何從Javascript中的多維阿拉爾獲取元素
for (var i = 0; i < mainArr.length; i++) {
var p = document.createElement('P');
for (var j = 0; j < mainArr[i].length; j++) {
element = mainArr[i][j];
p.appendChild(element);
}
$('#keyPad').append(p);
}
我希望這樣的輸出:
<p>
<input type="button" class="sil" value="C" style="width:50px">
<input type="button" class="hepsiniSil" value="AC" style="width:50px">
</p>
<p>
<input type="button" class="buttons" value="7">
<input type="button" class="buttons" value="8">
<input type="button" class="buttons" value="9">
<input type="button" class="buttons" value="*" >
</p>
<p>
<input type="button" class="buttons" value="4">
<input type="button" class="buttons" value="5">
<input type="button" class="buttons" value="6">
<input type="button" class="buttons" value="-">
</p>
<p>
<input type="button" class="buttons" value="1">
<input type="button" class="buttons" value="2">
<input type="button" class="buttons" value="3">
<input type="button" class="buttons" value="+">
</p>
<p>
<input type="button" class="buttons" value="0">
<input type="button" value="=" style="width:50px" onclick='esittir()'>
<input type="button" class="buttons" value="/">
</p>
在下面有這樣的:
$(document).ready(function() {
var butArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '+', '-', '*', '/', '=', 'C', "AC"];
var mainArr = [];
$('.createBut').click(function() {
var but = document.createElement("input");
var butClass = $('#inputClass').val();
var butValue = $('#inputValue').val();
var butRow = $('#inputRow').val();
var butColumn = $('#inputColumn').val();
var butWidth = $('#inputWidth').val();
var butHeigth = $('#inputHeigth').val();
var butClick = $('#inputClick').val();
but.type = 'button';
but.value = butValue;
but.className = butClass;
but.style.width = butWidth;
but.style.height = butHeigth;
//but.onclick = butClick;
for (var i = 0; i < 17; i++) {
mainArr[butRow] = [];
for (var j = 0; j < 17; j++) {
mainArr[butRow][butColumn] = but;
}
}
});
請分享你的'mainArr'內容 –
我共享@CommercialSuicide –
你是不是盡顯你的2D按鈕排列正確。另外,您必須爲每個數組單元格創建一個單獨的按鈕元素,否則您將多次添加相同的按鈕,這樣只會在最後添加的父項下生成一個按鈕。 – Redu