Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在HTML正文中调用JavaScript函数_Javascript_Html - Fatal编程技术网

如何在HTML正文中调用JavaScript函数

如何在HTML正文中调用JavaScript函数,javascript,html,Javascript,Html,我有一个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 javas

我有一个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>

var col1=[“全日制学生支票(22岁及以下)”,“65岁以上的客户”,“500.00美元以下”];
var col2=[“无”、“无”、“8.00美元”];
函数createtable(){
对于(var j=0;j
我想在HTML正文中执行它。我尝试了以下方法,但它没有创建表

<table>
    <tr>
        <th>Balance</th>
        <th>Fee</th>        
    </tr>
      createtable();
</table>

平衡
费用
createtable();

如何在HTML正文中执行此函数?

尝试包装
createtable()
标记中的code>语句:

<table>
        <tr>
            <th>Balance</th>
            <th>Fee</th>

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

平衡
费用
createtable();

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

尝试在脚本标记中使用DOM的createChild()方法或表对象的insertRow()和insertCell()方法。

首先在html的head标记中包含文件,然后在body标记下的script标记中调用函数,例如

要调用的Js文件函数

function tryMe(arg) {
    document.write(arg);
}
HTML文件

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src='object.js'> </script>
    <title>abc</title><meta charset="utf-8"/>
</head>
<body>
    <script>
    tryMe('This is me vishal bhasin signing in');
    </script>
</body>
</html>

abc
tryMe(“我是vishal bhasin登录”);

完成

只是为了澄清问题,您不能“在HTML正文中执行它”

您可以使用javascript修改HTML的内容

您可以决定在什么时候执行javascript

例如,下面是一个html文件的内容,包括javascript,它可以满足您的需要

<html>
  <head>
    <script>
    // The next line document.addEventListener....
    // tells the browser to execute the javascript in the function after
    // the DOMContentLoaded event is complete, i.e. the browser has
    // finished loading the full webpage
    document.addEventListener("DOMContentLoaded", function(event) { 
      var col1 = ["Full time student checking (Age 22 and under) ", "Customers over age 65", "Below  $500.00" ];
      var col2 = ["None", "None", "$8.00"];
      var TheInnerHTML ="";
      for (var j = 0; j < col1.length; j++) {
        TheInnerHTML += "<tr><td>"+col1[j]+"</td><td>"+col2[j]+"</td></tr>";
    }
    document.getElementById("TheBody").innerHTML = TheInnerHTML;});
    </script>
  </head>
  <body>
    <table>
    <thead>
      <tr>
        <th>Balance</th>
        <th>Fee</th>        
      </tr>
    </thead>
    <tbody id="TheBody">
    </tbody>
  </table>
</body>

//下一行document.addEventListener。。。。
//告诉浏览器在完成后在函数中执行javascript
//DOMContentLoaded事件已完成,即浏览器已
//已完成加载完整网页
addEventListener(“DOMContentLoaded”,函数(事件){
var col1=[“全日制学生支票(22岁及以下)”,“65岁以上的客户”,“500.00美元以下”];
var col2=[“无”、“无”、“8.00美元”];
var theinerhtml=“”;
对于(var j=0;j


享受吧

为什么要把它放在javascript函数中?您使用JavaScript而不是仅在HTML中创建表是否有特定的原因?为什么不接受答案?是否有可能按照上述建议,在呈现页面时立即调用JavaScript函数?因此,当页面上的脚本标记应该调用函数时,情况就不是这样了。我也在使用vue.js,所以这可能会导致我的意外行为。我只想报告一下:我发现了我的方法的错误。我想在另一个选项卡上调用该函数,与所有vue.js应用程序一样,它位于同一个“页面”上,因此实际上这完全是预期的行为。