[英]Firebug is returning: document.getElementById(…) is null -what am I doing wrong?
I'm trying to create a unit conversion calculator for a three-ingredient recipe, but can't get the calculations to run through after hitting 'get ingredients' button. 我正在尝试为三种成分的配方创建一个单位换算计算器,但是在单击“获取配料”按钮后无法进行计算。 Firebug is returning the getElementById lines as null?
Firebug将getElementById行返回为null? JS:
JS:
$(function(){
$('#tabs').tabs();
});
window.onload = init;
function init(){
document.getElementById("calc").onclick=function(){
var pbc=Number(document.getElementById("pbc").value);
var dc=Number(document.getElementById("dc").value)* .0333333333;
var pb=Number(document.getElementById("pb").value)* .0166666667;
var sea=Number(document.getElementById("sea").value)* .0020833333;
var dcTotal= ("dc" * "pbc");
var pbTotal= ("pb" * "pbc");
var seaTotal= ("sea" * "pbc");
document.getElementById("resultdc").innerHTML=dcTotal.toFixed(1);
document.getElementById("resultpb").innerHTML=pbTotal.toFixed(1);
document.getElementById("resultsea").innerHTML=seaTotal.toFixed(1);
}
}
portion of HTML: HTML部分:
<form action="javascript:void(0)">
<label for="pbc">How many Dark Chocolate Peanut Butter Cups would you like `to make?</label>`
<input type="text" name="pbc" id="pbc">
<br><br>
<input type="button"id="calc" value="Get Ingredients">
</form>
<p id="resultdc"> Cup(s) of Dark Chocolate Chips</p>
<p id="resultpb"> Cup(s) of Peanut Butter</p>
<p id="resultsea"> Teaspoon(s) of Sea Salt</p>
在"button"
和id
之间需要一个空格
<input type="button" id="calc" value="Get Ingredients">
by putting quotes around your variables, you ar econverting them into strings and therefore not able to be used for calculations: 通过在变量周围加上引号,您可以将它们转换为字符串,因此不能用于计算:
eg 例如
var dcTotal= ("dc" * "pbc");
var pbTotal= ("pb" * "pbc");
var seaTotal= ("sea" * "pbc");
should be 应该
var dcTotal= (dc * pbc);
var pbTotal= (pb * pbc);
var seaTotal= (sea * pbc);
also note that the following line 还请注意以下行
document.getElementById("resultdc").innerHTML=dcTotal.toFixed(1);
will replace all content in the ("resultdc") div, with the value (dcTotal.toFixed(1)) 将值(dcTotal.toFixed(1))替换(“ resultdc”)div中的所有内容
you should change your html to something like.. 您应该将html更改为类似。
<p><span id="resultdc"></span> Cup(s) of Dark Chocolate Chips</p>
which will fill in the desired number into the span and not replace the text content. 它将在跨度中填充所需的数字,而不替换文本内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.