gpt4 book ai didi

javascript - 将 Cm 转换为 ft 值并在其他元素中显示会给出引用错误

转载 作者:行者123 更新时间:2023-12-03 03:56:16 26 4
gpt4 key购买 nike

我的转换数学是正确的(在这里查找),但是从显示高度(以厘米为单位)的元素中获取值,然后将其解析为英尺/英寸并将其显示在(单击时)右手跨度上不起作用,我收到引用错误(转换器未定义)。

我不明白为什么它是未定义的,是因为提升还是parseInt函数没有参数?

这是函数

var displayInches = document.getElementById("heightInches");


displayInches.addEventListener("click", function() {
toFeet(converter);
});

function toFeet(converter) {
var heightOutputCM = document.getElementById("yourHeight");
var converter = parseInt(heightOutputCM.value);
var realFeet = converter * 0.3937 / 12;
var feet = Math.floor(realFeet);
var inches = Math.round((realFeet - feet) * 12);
return feet + "and" + inches;
}

这是链接:

https://codepen.io/damianocel/pen/ZyRogX

最佳答案

HTML

<h1>Alcohol blood level calculator</h1>

<fieldset>
<legend>Your Indicators</legend><br>
<label for="height" class="margin">Height:</label>
<span class="leftlabel" id=""><span id="yourHeight"></span>Cm</span>
<input type="range" id="UserInputHeight" name="height" min="0" max="200" step="1" style="width: 200px">
<span id="heightInchesSpan" class="rightlabel"><span id="heightInches"></span>Ft</span>
<br>
<label for="" class="margin">Gender:</label>
<span class="leftlabel">Male</span>
<input type="range" id="salary" name="salary" min="0" max="1" style="width: 200px">
<span class="rightlabel">Female</span>
</fieldset>

JS

// get and display height

var displayHeightInput = document.getElementById("UserInputHeight");

displayHeightInput.addEventListener("input", function() {
sliderChange(this.value);
});

function sliderChange(val) {
var heightOutput = document.getElementById("yourHeight");
heightOutput.innerHTML = val;
toFeet();
return val;
}


function toFeet() {
var heightOutputCM = document.getElementById("yourHeight");
var converter = parseInt(heightOutputCM.innerHTML);
var realFeet = converter * 0.3937 / 12;
var feet = Math.floor(realFeet);
var inches = Math.round((realFeet - feet) * 12);
document.getElementById("heightInches").innerHTML=feet + "and" + inches;
return feet + " and " + inches;
}

关于javascript - 将 Cm 转换为 ft 值并在其他元素中显示会给出引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44930917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com