我创建了数组,并使其在单击“提交”按钮时可以查看。下面是我编写的程序代码。但是问题是当打开html文件arrayusingforloop1.html时,甚至在单击Submit按钮之前显示了一个小方表。但是我只想显示Submit按钮而不是那个小表。然后,单击“提交”按钮时,将显示要显示表格的数组。因此,请给我解决方案。

arrayusingforloop1.html的代码:


    

<script>
    function array() {
        var table1 = "<table id='tabl123' ><thead></thead>";
        table1 += "<tr><label id='lb1'></label></tr><BR><tr><label id='lb2'></label></tr><BR><tr><label id='lb3'></label></tr><br><tr><label id='lb4'></label></tr><br><tr><label id='lb5'></label></tr><br><tr><label id='lb6'></label></tr><br><tr><label id='lb7'></label></tr><br><tr><label id='lb8'></label></tr><br><tr><label id='lb9'></label></tr><br><tr><label id='lb10'></label></tr></table>";

        var tablabc = document.getElementById("dynamic");
        tablabc.innerHTML = table1;





        arr = ["&nbspLee &nbsp", "22", "cse"];
        text = "";
        var i;
        for (i = 0; i < arr.length; i++) {
            text += " " + arr[i] + " ";
        }

        document.getElementById("lb1").innerHTML = text;


        arr1 = ["&nbspram       &nbsp", "22", "cse"];
        text1 = "";
        var a;
        for (a = 0; a < arr1.length; a++) {
            text1 += " " + arr1[a] + " ";
        }

        document.getElementById("lb2").innerHTML = text1;


        arr2 = ["&nbspmei       &nbsp", "22", "cse"];
        text2 = "";
        var c;
        for (c = 0; c < arr2.length; c++) {
            text2 += " " + arr2[c] + " ";
        }

        document.getElementById("lb3").innerHTML = text2;


        arr3 = ["&nbspJas       &nbsp", "22", "cse"];
        text3 = "";
        var d;
        for (d = 0; d < arr3.length; d++) {
            text3 += " " + arr3[d] + " ";
        }

        document.getElementById("lb4").innerHTML = text3;


        arr4 = ["&nbspDpu    &nbsp", "22", "cse"];
        text4 = "";
        var e;
        for (e = 0; e < arr4.length; e++) {
            text4 += " " + arr4[e] + " ";
        }

        document.getElementById("lb5").innerHTML = text4;

        arr5 = ["&nbspBav      &nbsp", "21", "cse"];
        text5 = "";
        var f;
        for (f = 0; f < arr5.length; f++) {
            text5 += " " + arr5[f] + " ";
        }

        document.getElementById("lb6").innerHTML = text5;

        arr6 = ["&nbspjai          &nbsp", "22", "cse"];
        text6 = "";
        var g;
        for (g = 0; g < arr6.length; g++) {
            text6 += " " + arr6[g] + " ";
        }

        document.getElementById("lb7").innerHTML = text6;

        arr7 = ["&nbspVic       &nbsp", "22", "cse"];
        text7 = "";
        var h;
        for (h = 0; h < arr5.length; h++) {
            text7 += " " + arr7[h] + " ";
        }

        document.getElementById("lb8").innerHTML = text7;

        arr8 = ["&nbsplav       &nbsp", "22", "cse"];
        text8 = "";
        var j;
        for (j = 0; j < arr8.length; j++) {
            text8 += " " + arr8[j] + " ";
        }

        document.getElementById("lb9").innerHTML = text8;

        arr9 = ["&nbspSid       &nbsp", "22", "cse"];
        text9 = "";
        var k;
        for (k = 0; k < arr5.length; k++) {
            text9 += " " + arr9[k] + " ";
        }

        document.getElementById("lb10").innerHTML = text9;


    }
</script>
</head>
<body>
    <style>
    #dynamic {
        padding: 15px;
        height: 50px;
        width: "100%";
        border-style: solid;
        border-width: 5px;
        background-color: yellow;
        color: red;
    }

    tr {
        border: 1px solid black;
        border-style: solid;
        border-width: 5px;
    }


    #tabl123 {
        padding: 15px;
        height: 50px;
        width: "100%";
        border: 1px solid black;
        border-style: solid;
        border-width: 5px;
        color: red;
    }
</style>
<table id="dynamic" border="1" />

<button onclick="array()">submit</button>





请给我解决方案。

最佳答案

一个简单的解决方案是在表中设置display:none样式,然后在“ array()”函数的末尾删除该样式。

Demo

关于javascript - Javascript使用For Loop创建带有要在其中显示的数组的动态表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28211926/

10-10 11:45
查看更多