Вот мой код:
Код: Выделить всё
document.getElementById("confirm").addEventListener("click", function () {
const inputTemp = parseFloat(document.getElementById("inputTemp").value);
const inputUnit = document.getElementById("inputUnit").value;
const outputUnit = document.getElementById("inputUnit").value;
const conversions = {
Celsius: {
Fahrenheit: (temp) => temp * 9 / 5 + 32,
Kelvin: (temp) => temp + 273.15,
Celsius: (temp) => temp,
},
Fahrenheit: {
Celsius: (temp) => (temp - 32) * 5 / 9,
Kelvin: (temp) => (temp - 32) * 5 / 9 + 273.15,
Fahrenheit: (temp) => temp,
},
Kelvin: {
Celsius: (temp) => temp - 273.15,
Fahrenheit: (temp) => (temp - 273.15) * 9 / 5 + 32,
Kelvin: (temp) => temp,
},
}
const result = conversions[inputUnit][outputUnit](inputTemp);
document.getElementById("outputTemp").textContent = String(result || 0);
});Код: Выделить всё
body {
font-family: Montserrat, sans-serif;
display: flex;
justify-content: center;
place-items: center;
height: 100vh;
background-color: #F4F4F9;
}
.container {
background-color: #ffffff;
padding: 20px 30px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
text-align: center;
}
h1 {
margin-bottom: 20px;
font-size: 24px;
}
.inputs, .outputs {
margin-bottom: 15px;
}
input, select, output, button {
padding: 10px;
margin: 5px;
border: 1px solid #ddd;
border-radius: 8px;
font-size: 14px;
transition: 0.5s ease-in-out;
}
button {
background-color: #007bff;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #0056b3
}Код: Выделить всё
Temperature | Unit Converter
Celsius
Fahrenheit
Kelvin
Celsius
Fahrenheit
Kelvin
0
Convert
Я подумал, что это может быть связано с тем, что DOM не загружается раньше элементы загружены, поэтому я попытался добавить их в начало кода JS:
Код: Выделить всё
document.addEventListener("DOMContentLoaded", function () {Подробнее здесь: https://stackoverflow.com/questions/793 ... g-on-click
Мобильная версия