简体   繁体   中英

Write to HTML file from Javascript

I know it's been asked before, but I can't seem to figure out any way of doing this. I return a 2D array from a Java method and want to print the results into a table. I have table headers already declared above it. I call createT() after somebody submits a previously defined form. The array the Java method returns is correct, I just can't seem to print off the table.

<table>
    ...
    <tbody>
        <script>
            function createT(){
                <% int[][]x = query.getData(); %>
                var result;
                for(var i=0; i<x.length; i++) {
                    results+= "<tr bgcolor = 'red'>";
                    for(var j=0; j<x[i].length; j++){
                        result+="<td>"+x[i][j]+"</td>";
                    }
                    result += "</tr>";
                }
                document.write(result);
            }
        </script>
    </tbody>
</table>

document.write will delete all existing HTML.

You can try this

document.getElementById('YourtableId').appendChild(result);

Also create elements using document.createElement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM