本文关联的程序可根据输入的身高和体重计算体质指数,低于18偏瘦,18-24正常,高于24偏胖。
下面是程序源代码:
function myshow(){ var outStr=""; var cm = parseFloat(document.getElementById('cm').value); if(isNaN(cm)) cm=0; var kg = parseFloat(document.getElementById('kg').value); if(isNaN(kg)) kg=0; if(navigator.userAgent.toLowerCase().indexOf("windows")==-1) outStr+='<div style="font-size:10px">'; if(cm>9.9) outStr+="身高"+cm.toFixed(1)+"cm的最佳体重范围是"+(0.0018*cm*cm).toFixed(1)+"kg至"+(0.0024*cm*cm).toFixed(1)+"kg"; outStr+="<br>"; if(kg>2.9) outStr+="体重"+kg.toFixed(1)+"kg的最佳身高范围是"+Math.sqrt(kg/0.0024).toFixed(1)+"cm至"+Math.sqrt(kg/0.0018).toFixed(1)+"cm"; outStr+="<br>"; if(navigator.userAgent.toLowerCase().indexOf("windows")==-1) outStr+='</div>'; if(cm<10.0){ outStr+="请输入身高(cm)"; document.getElementById('outDiv').innerHTML=outStr; return; } if(kg<3.0){ outStr+="请输入体重(kg)"; document.getElementById('outDiv').innerHTML=outStr; return; } cm/=100; kg/=cm*cm; outStr+="体质指数:" + kg.toFixed(1); document.getElementById('outDiv').innerHTML=outStr; } document.getElementById("cm").type="number"; document.getElementById("kg").type="number"; document.getElementById("cm").placeholder="身高(cm)"; document.getElementById("kg").placeholder="体重(kg)"; document.getElementById("cm").onkeyup = myshow; document.getElementById("kg").onkeyup = myshow; if(navigator.userAgent.toLowerCase().indexOf("windows")==-1) myshow();