[英]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:我看到的两个修复:
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.