简体   繁体   English

我使用 javascript 在 HTML 中创建了一个表格

[英]I have created a table in HTML using javascript

wiubWEFBWieufbwfeiubwefubfjdbffhejj ehjfj

The codepen helped immensely! codepen 帮了大忙! I mentioned looking at the console output at one point.我曾经提到过查看控制台输出。 That's critically important because it's telling you what went wrong, so make sure you know how to see it .这非常重要,因为它会告诉您出了什么问题,因此请确保您知道如何看待它 In codepen its at the bottom left as well, and there's a red exclamation mark in the JS showing that an error is present.在 codepen 中,它也在左下角,并且 JS 中有一个红色的感叹号,表明存在错误。 When I hit the Metric button I see:当我点击公制按钮时,我看到:

TypeError: obj.height is null类型错误:obj.height 为空

That's because some of your data has null for width or height instead of the value you're expecting, so it "crashes" and just stops at whatever row caused the problem.那是因为您的某些数据的宽度或高度为 null,而不是您期望的值,因此它“崩溃”并在导致问题的任何行处停止。 That's why some rows disappeared.这就是为什么有些行消失了。

Two fixes I see:我看到的两个修复:

  • Fix your data so that the values don't have null in them修复您的数据,以便其中的值不包含 null
  • Make the code more robust so it doesn't crash on invalid data (this is always preferable for any program).使代码更健壮,这样它就不会因无效数据而崩溃(这对于任何程序来说总是可取的)。

For the second option, you can fix the code like this:对于第二个选项,您可以像这样修复代码:

function MetricValues() { 
    var tab = document.querySelector("table");
    tab.innerHTML = "";
    for (var obj of death_row) {
        var height = obj.height || "0' 0\"";
        var row = `<tr><td>${obj.first_name}</td>
         <td>${obj.last_name}</td>
         <td>${obj.age_at_execution}</td>
         <td>${((obj.weight)/2.2046).toFixed(1)}</td>
         <td>${(((Number(height[0])*12*2.54) + (Number(height[3])*2.54))/100).toFixed(2)}</td></tr>`;
        tab.innerHTML += row;
    }
};

Using a temporary variable var height = obj.height || "0' 0\\"";使用临时变量var height = obj.height || "0' 0\\""; var height = obj.height || "0' 0\\""; which defaults to 0' 0" fixes the crash. var height = obj.height || "0' 0\\"";默认为0' 0"修复崩溃。 BTW you definitely should read up on functions .顺便说一句,你绝对应该阅读函数

暂无
暂无

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

相关问题 如何使用 javascript 填充由 Django 创建的 HTML 表? - How can I populate an HTML table created by Django using javascript? 如何删除使用 Javascript 创建的 HTML 元素 - how do I remove HTML element that I have created using Javascript 如何使用javascript中创建的对象在html中创建表? - How to create a table in html using an object created in javascript? 如何使用 javascript 下拉列表将 append 已经创建的 html 表格放入表格中? - How to append already created html form into table using javascript dropdown? 我创建了一个只使用 JavaScript 的包含一些记录的表,我想将我的数组与对象设置为本地存储 - I have created a table with some records using with JavaScript only, I want to set my array with objects to local Storage 在dojo和javascript中填充动态创建的html表 - populate dynamically created html table in dojo and javascript 如何使用javascript在没有按钮的情况下在html表中执行计算(表是在输入值上创建的)? - How to perform calculation in html table(table is created on input values) without button using javascript? 在HTML表格上使用Javascript? - Using Javascript on an HTML table? 我有一个用PHP代码创建的HTML问题 - I have an issue with HTML that created with php code 在 javascript 验证后,我无法加载我作为学生创建的另一个 html 页面。html - I am not able to load another html page which I have created as student.html after javascript validation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM