我是web dev的新手,最近我学习了DOM。 但是,我遇到了一个困扰我的问题。 我正在尝试制作一个简单的球体积计算器,用户可以通过输入半径来获得球体积。无法从DOM中的输入文本中获取值
这是代码。
HTML
Enter the sphere radius : <input type="number" id="radius">
<button id=>Calculate !</button>
<p>Therefore, the volume of sphere are: <span id="volsphere"></span> </p>
JS
var radius = document.querySelector("#radius");
var volsphere = document.querySelector("#volsphere");
volsphere.addEventListener("click",function(){
//calculate the sphere volume
var spherevolume = (4/3) * Math.PI * Math.pow(radius,3);
//put the text content into sphere volume
volsphere.textContent = spherevolume ;
});
我尝试排除故障控制台日志radius.value和spherevolume.value工作吧。
半径似乎是罚款,给我“3”,但球体体积有
VM97:1 Uncaught ReferenceError: spherevolume is not defined at :1:1
所以此错误信息,这部分代码是给了这个错误? 谢谢那些帮助
你得点击收听的,你应该把它放在按钮。你没有点击跨度,是吗? – Meggg
你的标记看起来无效'' 您应该为按钮的id属性填写值或完全删除属性。 – tommyO
@Megg我的错误。我应该把监听器事件放在按钮上 –