ここでは、当社の新人研修受講者に向けて、JavaScriptのサンプルコードを紹介しています。
摂氏と華氏の変換器のサンプルコード
スライダーを動かすと摂氏(0~100)と華氏の温度を表示します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<!DOCTYPE html> <html> <head> <title>c2f</title> <meta charset="UTF-8"> <script> function c2f(c) { var f = (9 / 5) * c + 32; return Math.floor(f); } function conv(){ var c = document.getElementById("celsius").value; var f = c2f(c); var s = "Celsius:"+c+"°C" +" = Fahrenheit:"+f+"°F" ; document.getElementById("result").textContent = s; } </script> </head> <body onload="conv()"> <input type="range" id ="celsius" value ="0" onchange="conv()"> <p id ="result"></p> </body> </html> |