1

良い一日。ネストされたテーブルを 100 回作成しようとしています。ただし、私のコードは 1 つのメイン テーブルを作成し、その中に 100 個の個別のテーブルがあります。(Sir Sachin の助けに感謝します) 私が必要としているのは、テーブル内のテーブルです。コードの修正を手伝ってください。

<html>
<head> <title> Hello! </title> 

<script type="text/javascript">
function add() { 
    var ni = document.getElementById('myDiv');
    var numi = document.getElementById('theValue');
    var num = (document.getElementById('theValue').value -1)+ 2;
    numi.value = num;
    var newdiv = document.createElement('div');
    var divIdName = 'my'+num+'Div';
    newdiv.setAttribute('id',divIdName);
    newdiv.innerHTML = "<table border=1><tr><td> Hello! <input type='hidden' value=1 id='theValue' /><div id='" + divIdName + "'></td></tr></table>";
    ni.appendChild(newdiv);
    for(var i=1;i<100;i++) {
        var ni = document.getElementById(divIdName);
        var numi = document.getElementById('theValue');
        var num = (document.getElementById('theValue').value -1)+ 2;
        numi.value = num;
        var newdiv = document.createElement('div');
        var divIdName = 'my'+num+'Div';
        newdiv.setAttribute('id',divIdName);
        var j=i++;
        newdiv.innerHTML = "<table border=1><tr><td> Hello! <input type='hidden' value='" + j + "' id='theValue' /><div id='" + divIdName + "'></td></tr></table>";
        ni.appendChild(newdiv);
    }
}
</script>

</head>
<body onload="add()">
<table border="1">
    <tr>
        <td> Hello! <input type='hidden' value='0' id='theValue' />

        <div id='myDiv'> </div> </td>
    </tr>
</table>
</body>
</html>
4

1 に答える 1

0

これでうまくいくはずです(隠し入力は不要なので削除しました): http://jsfiddle.net/rPR9w/1/

function add() {
    for (i=1; i<=100; i++) {
        addAnother(i);
    }
}


function addAnother(counter) { 
    var container = document.getElementById('myDiv' + counter);
    container.innerHTML = makeTable(counter + 1);
}

function makeTable(counter) {

    return "<table border=1><tr><td> Hello! <div id='myDiv" + counter + "'></td></tr></table>";

}
于 2013-03-01T16:37:38.723 に答える