如何在HTML主体中调用JavaScript函数 [英] How to call a JavaScript function within an HTML body

查看:100
本文介绍了如何在HTML主体中调用JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个填充表的JavaScript函数:

 < script> 
$ b $ var col1 = [全日制学生检查(22岁及以下),65岁以上的客户,500.00美元以下];
var col2 = [None,None,$ 8.00];

函数createtable(){
<! - 用javascript填充表格 - > $ j $ b $ 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>余额< / th>
< th> Fee< / th>
< / tr>
createtable();
< / table>

如何在HTML体内执行此函数?

createtable(); 语句包装在< script>中 code> tag:

 < table> 
< tr>
< th>余额< / th>
< th> Fee< / th>

< / tr>
< script> createtable();< / script>
< / table>

如果我是你,我会避免使用document.write()并使用DOM。 p>

I have a JavaScript function that fills a table:

<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. 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?

解决方案

Try wrapping the createtable(); statement in a <script> tag:

<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.

这篇关于如何在HTML主体中调用JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆