Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

javascript - 如何在HTML正文中调用JavaScript函数(How to call a JavaScript function within an HTML body)

I have a JavaScript function that fills a table:(我有一个填充表格的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> I want to execute it within the HTML body.(我想在HTML正文中执行它。) I have tried the following, but it doesn't create the table.(我尝试了以下,但它没有创建表。) <table> <tr> <th>Balance</th> <th>Fee</th> </tr> createtable(); </table> How I can execute this function within the HTML body?(我如何在HTML体内执行此功能?)   ask by user2804038 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try wrapping the createtable();(尝试包装createtable();)

statement in a <script> tag:(<script>标记中的语句:) <table> <tr> <th>Balance</th> <th>Fee</th> </tr> <script>createtable();</script> </table> I would avoid using document.write() and use the DOM if I were you though.(如果我是你,我会避免使用document.write()并使用DOM。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...