第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript - 自動計算體重指數(shù)

Javascript - 自動計算體重指數(shù)

人到中年有點甜 2021-12-23 19:51:28
為了計算身體質量指數(shù),我有這個 javascript,其中有兩個用于身高和體重的輸入字段和一個用于顯示計算輸出的輸入字段。如何在 BMI 字段中自動顯示 che calc?商標<h2>Height=<input type="text" id="hgt"></h2><h2>Weight=<input type="text" id="wgt" onmouseout="bmi()"></h2><h2>BMI=<input type="text" id="student_bmi">Javascript<script>function bmi(){var sheight=parseFloat(document.getElementById('hgt').value);var sweight=parseFloat(document.getElementById('wgt').value);var bmi=sweight/Math.pow(sheight,2);var student_bmi=document.getElementById('student_bmi').value;student_bmi.textContent=bmi.toFixed(2);}</script>在 Height 和 Weight 字段中插入值后,不會在 BMI 字段中顯示計算值。如何解決這個問題?
查看完整描述

1 回答

?
慕尼黑5688855

TA貢獻1848條經(jīng)驗 獲得超2個贊

該onmouseout事件僅在鼠標位于元素內(nèi)部時觸發(fā),然后鼠標移出元素。當您計算 BMI 時,這似乎是一個奇怪的選擇,因為在典型的用戶操作期間,鼠標可能不會移動到輸入之外。


一種更直接的方法是在上述兩個輸入中任何一個的內(nèi)容發(fā)生變化時更新 BMI。您還應該考慮不在 HTML 中使用內(nèi)聯(lián) JavaScript 事件處理程序。這是一種不同的方法:


HTML:


<h2>Height=<input type="text" id="hgt"></h2>


<h2>Weight=<input type="text" id="wgt"></h2>


<h2>BMI=<input type="text" id="student_bmi"></h2>

JavaScript:


const heightInput = document.getElementById('hgt');

const weightInput = document.getElementById('wgt');

const bmiInput = document.getElementById('student_bmi');


heightInput.addEventListener('input', calculateBMI);

weightInput.addEventListener('input', calculateBMI);


function calculateBMI () {

  const height = parseFloat(heightInput.value);

  const weight = parseFloat(weightInput.value);

  const bmi = weight / Math.pow(height, 2);

  bmiInput.value = bmi.toFixed(2);

}

與input事件不同,該mouseout事件將在每次擊鍵、粘貼或其他輸入更改事件時觸發(fā)。您還可以使用該change事件或blur取決于您希望用戶體驗的樣子。


查看完整回答
反對 回復 2021-12-23
  • 1 回答
  • 0 關注
  • 257 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號