简体   繁体   English

javascript 函数不显示 html 表

[英]javascript function doesn't show html table

I tried to make a html table using java script我尝试使用 java 脚本制作一个 html 表格

function CreateTable(data) {
  alert(data);
  /*
  1 - Loop Through Array & Access each value
  2 - Create Table Rows & append to table
  */
  for (var i in data) {
    var row = `<tr>
                    <td>${data[i].Billing_Count}</td>
                    <td>${data[i].Flag_Type}</td>
</tr>
            `
    var table = $("#table-body")
    table.append(row)
  }
}

and my html :和我的 html :

<div id="chartdiv">
  <table id="our-table">
    <thead>
      <tr>
        <th>میزان مصرف</th>
        <th>نوع مشترک</th>
      </tr>
    </thead>
    <tbody id="table-body">

    </tbody>
  </table>
</div>

i used the alert() in my js to see if my data can be read, and it was ok.我在我的 js 中使用了alert()来查看我的数据是否可以读取,并且没问题。 but still there were no result in my web page and the table-body's div.但在我的网页和表格主体的 div 中仍然没有结果。 I also have no specific error我也没有具体的错误

Two issues in your code:您的代码中有两个问题:

  1. You are missing the id symbol ( # ) in the selector, should be $("#table-body") .您在选择器中缺少 id 符号 ( # ),应该是$("#table-body")

  2. You have to close the row with </tr> .您必须使用</tr>关闭该行。

You're missing a # in your selector.您的选择器中缺少#

$("#table-body") or document.querySelector("#table-body") will fix it. $("#table-body")document.querySelector("#table-body")将修复它。

https://codepen.io/Choppy/pen/eYzLMgL https://codepen.io/Choppy/pen/eYzLMgL

issue 1: - while creating the row, you missed out the end tr tag问题 1:-在创建行时,您错过了结束tr标记

issue 2: - $("table-body") - # symbol was missing.问题 2: - $("table-body") - #符号丢失。

 function CreateTable(data){ /* 1 - Loop Through Array & Access each value 2 - Create Table Rows & append to table */ for (var i in data){ var row = `<tr> <td>${data[i].Billing_Count}</td> <td>${data[i].Flag_Type}</td> </tr> ` var table = $("#table-body"); table.append(row) } } const data = [{Billing_Count: 'Count1', Flag_Type: 'Flap1'}, {Billing_Count: 'Count2', Flag_Type: 'Flap2'}, {Billing_Count: 'Count2', Flag_Type: 'Flap2'}] CreateTable(data);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="chartdiv"> <table id="our-table"> <thead> <tr> <th>میزان مصرف</th> <th>نوع مشترک</th> </tr> </thead> <tbody id="table-body"> </tbody> </table> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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