软件学习 1 month ago
parent 7f597e8186
commit 80aac95e5a

@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>进制转换器</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>进制转换器</h1>
<input type="text" id="input" placeholder="输入数字">
<label for="from-base">选择源进制:</label>
<select id="from-base">
<option value="2">二进制</option>
<option value="10">十进制</option>
<option value="8">八进制</option>
<option value="16">十六进制</option>
</select>
<label for="to-base">选择目标进制:</label>
<select id="to-base">
<option value="2">二进制</option>
<option value="10">十进制</option>
<option value="8">八进制</option>
<option value="16">十六进制</option>
</select>
<button id="convert">转换</button>
<h2>结果:</h2>
<p id="result"></p>
</div>
<script src="script.js"></script>
</body>
</html>

@ -0,0 +1,34 @@
document.getElementById('convert').addEventListener('click', function () {
const input = document.getElementById('input').value;
const fromBase = parseInt(document.getElementById('from-base').value);
const toBase = parseInt(document.getElementById('to-base').value);
// 将输入转换为十进制
let decimal;
try {
decimal = parseInt(input, fromBase);
if (isNaN(decimal)) throw new Error("无效输入");
} catch (error) {
document.getElementById('result').innerText = "输入无效,请检查!";
return;
}
// 将十进制转换为目标进制
let result;
switch (toBase) {
case 2:
result = decimal.toString(2);
break;
case 8:
result = decimal.toString(8);
break;
case 10:
result = decimal.toString(10);
break;
case 16:
result = decimal.toString(16).toUpperCase();
break;
}
document.getElementById('result').innerText = result;
});

@ -0,0 +1,66 @@
body {
font-family: Arial, sans-serif;
background-color: #eef2f3;
margin: 0;
padding: 20px;
}
.container {
max-width: 400px;
margin: auto;
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
color: #333;
}
h2 {
margin-top: 20px;
color: #555;
}
input,
select,
button {
width: 100%;
padding: 12px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
}
input:focus,
select:focus,
button:focus {
outline: none;
border-color: #007bff;
}
button {
background-color: #007bff;
color: white;
border: none;
cursor: pointer;
font-weight: bold;
transition: background-color 0.3s;
}
button:hover {
background-color: #0056b3;
}
#result {
padding: 10px;
background-color: #f8f9fa;
border: 1px solid #ddd;
border-radius: 5px;
text-align: center;
font-size: 18px;
color: #333;
}
Loading…
Cancel
Save