Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

How to create a weight converter with HTML and JavaScript?


เพื่อสร้างเครื่องมือแปลงน้ำหนักด้วย HTML และ JavaScript โค้ดมีดังนี้

ตัวอย่าง

<!DOCTYPE html> <html> <head> <style>    body{       font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;    }    input,span{       font-size: 20px;    } </style> </head> <body> <h1>Weight Converter</h1> <h2>Type weight in kg to convert it into grams</h2> <p> <label>Kilogram</label> <input id="inputKG" type="number" placeholder="Kilogram" oninput="KgtoGConverter(this.value)" onchange="KgtoGConverter(this.value)"> </p> <p>Grams: <span id="outputGrams"></span></p> <script>    function KgtoGConverter(weight) {       document.getElementById("outputGrams").innerHTML=weight*1000;    } </script> </body> </html>

ผลลัพธ์

โค้ดข้างต้นจะสร้างผลลัพธ์ดังนี้ -

How to create a weight converter with HTML and JavaScript?

เมื่อพิมพ์น้ำหนักเป็นกิโลกรัม -

How to create a weight converter with HTML and JavaScript?