[英]I want to ask about getElementbyId
In my code, I wanted to show the following text: "(User) is 153cm tall and weighs 37 kilograms. That qualifies as underweight" 在我的代码中,我想显示以下文本:“(用户)身高153厘米,重37公斤。这相当于体重不足”
My code is as below. 我的代码如下。 It's not supposed to work but some magic it works except for name.
它不应该起作用,但是除了名称之外,它还可以起作用。
function gogogo() {
let x=document.getElementById("height").value/100;
let y=document.getElementById("weight").value;
let BMI=y/Math.pow(x,2);
document.getElementById("output").innerText = Math.round(BMI*100)/100;
let answer;
if (BMI<18.5)
answer = "Underweight";
else if (BMI<=25)
answer = "Normal";
else if (BMI<=30)
answer = "Obese";
else
answer = "Overweight";
document.getElementById("answer").innerText = `"${name} is ${height.value} tall and ${weight.value} kilograms. That qualifies as ${answer}"`;
}
instead of 代替
document.getElementById("height")`
Apparently one can simply write: 显然一个人可以简单地写:
height
What's the problem? 有什么问题? Can you guys help me to fix it?
你们能帮我解决吗?
Javascript traverses the DOM as well insearch of an uninitialized variable. Javascript遍历DOM并搜索未初始化的变量。 If it finds a
node
with the same id
as the variable name, it takes the variable as the reference to that node
. 如果找到与变量名具有相同
id
的node
,则将变量作为对该node
的引用。
Example 例
<div>
<h1 id=title>My title</h1>
<input type=text value="mi text" id=yourText />
</div>
Since the id
s ['title','yourText']
appear in the DOM tree, you can call them directly ('Though not adviced'). 由于
id
s ['title','yourText']
出现在DOM树中,因此您可以直接调用它们(“尽管不建议”)。
So, calling title.innerText
, would return " My title
". 因此,调用
title.innerText
,将返回“ My title
”。 But calling an uninitialized variable whose id is not in the DOM as well, will give you an undefined
value 但是,调用ID不在DOM中的未初始化变量也会给您带来
undefined
值
您可以尝试使用innerHTML
代替innertext
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.