私は次のhtmlを持っています:
<table border="1" width="200">
<tr id="tr1">
<td id="TD">1</td>
<td id="TD">5</td>
<td id="TD">10</td>
</tr>
<tr id="tr2">
<td id="TD">$1</td>
<td id="TD">$2</td>
<td id="TD">$3</td>
</tr>
</table>
<button onclick="minus()">Back</button>
<button onclick="plus()">Forward</button>
そして、私は次の配列を持っています:
var specials = [
{ qty: '1', price: '1'},
{ qty: '5', price: '2'},
{ qty: '10', price: '3'},
{ qty: '20', price: '4'},
{ qty: '30', price: '5'},
{ qty: '40', price: '6'},
{ qty: '50', price: '7'}
];
html テーブルをオンロードすると、配列の最初の 3 つの項目が表示されます。2 つのボタンをクリックして項目を移動したいと考えています。したがって、「進む」をクリックすると、テーブルに次の項目が表示されます。
私は次のようなことを念頭に置いていました:
var cols = document.getElementById('tr1').getElementsByTagName('td'), colslen = cols.length;
var cols2 = document.getElementById('tr2').getElementsByTagName('td');
q = -1;
function plus() {
i = -1;
while(++i < colslen){
q = q+1;
cols[i].innerHTML = specials[q].qty;
cols2[i].innerHTML = "$"+specials[q].price;
}
}
しかし、1 つ進むのではなく 3 つ進むという点で、明らかに欠陥があります。つまり、オーバーラップが表示されません。
これについてもっと良い方法があると確信しています-そうですか?