最近更新于 2025-04-01 18:31
代码是直接用 AI 生成的
可以在线访问:https://iyatt.com/tools/temperature-converter/
也可以把下面代码复制粘粘到文本文件,然后把文件名命名为 index.html,直接双击用浏览器打开即可运行
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>温度转换器</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.container {
max-width: 400px;
margin: 0 auto;
text-align: center;
}
input {
margin: 10px 0;
padding: 5px;
width: 80%;
font-size: 16px;
}
</style>
</head>
<body>
<div class="container">
<h1>温度转换器</h1>
<div>
<label for="celsius">摄氏度 (°C):</label>
<input type="number" id="celsius" oninput="convertFromCelsius()">
</div>
<div>
<label for="fahrenheit">华氏度 (°F):</label>
<input type="number" id="fahrenheit" oninput="convertFromFahrenheit()">
</div>
<div>
<label for="kelvin">热力学温度 (K):</label>
<input type="number" id="kelvin" oninput="convertFromKelvin()">
</div>
</div>
<script>
function convertFromCelsius() {
const celsius = parseFloat(document.getElementById('celsius').value);
if (isNaN(celsius)) {
document.getElementById('fahrenheit').value = '';
document.getElementById('kelvin').value = '';
return;
}
const fahrenheit = (celsius * 9 / 5) + 32;
const kelvin = celsius + 273.15;
document.getElementById('fahrenheit').value = fahrenheit.toFixed(2);
document.getElementById('kelvin').value = kelvin.toFixed(2);
}
function convertFromFahrenheit() {
const fahrenheit = parseFloat(document.getElementById('fahrenheit').value);
if (isNaN(fahrenheit)) {
document.getElementById('celsius').value = '';
document.getElementById('kelvin').value = '';
return;
}
const celsius = (fahrenheit - 32) * 5 / 9;
const kelvin = celsius + 273.15;
document.getElementById('celsius').value = celsius.toFixed(2);
document.getElementById('kelvin').value = kelvin.toFixed(2);
}
function convertFromKelvin() {
const kelvin = parseFloat(document.getElementById('kelvin').value);
if (isNaN(kelvin)) {
document.getElementById('celsius').value = '';
document.getElementById('fahrenheit').value = '';
return;
}
const celsius = kelvin - 273.15;
const fahrenheit = (celsius * 9 / 5) + 32;
document.getElementById('celsius').value = celsius.toFixed(2);
document.getElementById('fahrenheit').value = fahrenheit.toFixed(2);
}
</script>
</body>
</html>
摄氏度、华氏度、热力学温度互转 网页实现