我有一个填充表格的JavaScript函数:
<script> var col1 = ["Full time student checking (Age 22 and under) ","Customers over age 65","Below $500.00"]; var col2 = ["None","None","$8.00"]; function createtable() { <!--To fill the table with javascript--> for (var j = 0; j < col1.length; j++) { if (j % 2 == 0) { document.write("<tr><td>" + col1[j] + " </td>"); document.write("<td>" + col2[j] + "</td></tr>"); } else { document.write("<tr bgcolor='#aeb2bf'><td>" + col1[j] + " </td>"); document.write("<td>" + col2[j] + "</td></tr1>"); } } } </script>
我想在HTML正文中执行它.我尝试了以下,但它没有创建表.
<table> <tr> <th>Balance</th> <th>Fee</th> </tr> createtable(); </table>
我如何在HTML体内执行此功能?
解决方法
尝试包装createtable(); < script>中的语句标签:
<table> <tr> <th>Balance</th> <th>Fee</th> </tr> <script>createtable();</script> </table>
如果我是你,我会避免使用document.write()并使用DOM.