|
|
|
@ -2,39 +2,123 @@
|
|
|
|
|
<html lang="en">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<title>基数转换器</title>
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
|
<title>进制转换器</title>
|
|
|
|
|
<style>
|
|
|
|
|
body {
|
|
|
|
|
font-family: 'Arial', sans-serif;
|
|
|
|
|
margin: 0;
|
|
|
|
|
padding: 20px;
|
|
|
|
|
background-color: #f7f7f7;
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
align-items: center;
|
|
|
|
|
height: 100vh;
|
|
|
|
|
}
|
|
|
|
|
.container {
|
|
|
|
|
background-color: #fff;
|
|
|
|
|
padding: 30px;
|
|
|
|
|
border-radius: 10px;
|
|
|
|
|
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
|
|
|
|
max-width: 400px;
|
|
|
|
|
width: 100%;
|
|
|
|
|
}
|
|
|
|
|
h1 {
|
|
|
|
|
text-align: center;
|
|
|
|
|
color: #333;
|
|
|
|
|
}
|
|
|
|
|
.form-group {
|
|
|
|
|
margin-bottom: 20px;
|
|
|
|
|
}
|
|
|
|
|
label {
|
|
|
|
|
display: block;
|
|
|
|
|
margin-bottom: 5px;
|
|
|
|
|
color: #666;
|
|
|
|
|
}
|
|
|
|
|
input[type="number"],
|
|
|
|
|
input[type="text"] {
|
|
|
|
|
width: 100%;
|
|
|
|
|
padding: 10px;
|
|
|
|
|
border: 1px solid #ddd;
|
|
|
|
|
border-radius: 5px;
|
|
|
|
|
box-sizing: border-box;
|
|
|
|
|
}
|
|
|
|
|
button {
|
|
|
|
|
width: 100%;
|
|
|
|
|
padding: 10px;
|
|
|
|
|
border: none;
|
|
|
|
|
border-radius: 5px;
|
|
|
|
|
background-color: #5cb85c;
|
|
|
|
|
color: white;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
transition: background-color 0.3s ease;
|
|
|
|
|
}
|
|
|
|
|
button:hover {
|
|
|
|
|
background-color: #4cae4c;
|
|
|
|
|
}
|
|
|
|
|
#result {
|
|
|
|
|
margin-top: 20px;
|
|
|
|
|
padding: 10px;
|
|
|
|
|
background-color: #dff0d8;
|
|
|
|
|
border: 1px solid #d6e9c6;
|
|
|
|
|
border-radius: 5px;
|
|
|
|
|
color: #3c763d;
|
|
|
|
|
}
|
|
|
|
|
@media (max-width: 480px) {
|
|
|
|
|
body {
|
|
|
|
|
padding: 10px;
|
|
|
|
|
}
|
|
|
|
|
.container {
|
|
|
|
|
padding: 20px;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<h1>基数转换器</h1>
|
|
|
|
|
<div class="container">
|
|
|
|
|
<h1>进制转换器</h1>
|
|
|
|
|
<form id="baseConverterForm">
|
|
|
|
|
<label for="sourceBase">源基数 (2-16):</label>
|
|
|
|
|
<div class="form-group">
|
|
|
|
|
<label for="sourceBase">初始进制(2-16):</label>
|
|
|
|
|
<input type="number" id="sourceBase" name="sourceBase" min="2" max="16" required>
|
|
|
|
|
<br><br>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="form-group">
|
|
|
|
|
<label for="valueToConvert">要转换的值:</label>
|
|
|
|
|
<input type="text" id="valueToConvert" name="valueToConvert" required>
|
|
|
|
|
<br><br>
|
|
|
|
|
<label for="targetBase">目标基数 (2-16):</label>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="form-group">
|
|
|
|
|
<label for="targetBase">目标进制(2-16):</label>
|
|
|
|
|
<input type="number" id="targetBase" name="targetBase" min="2" max="16" required>
|
|
|
|
|
<br><br>
|
|
|
|
|
</div>
|
|
|
|
|
<button type="submit">转换</button>
|
|
|
|
|
</form>
|
|
|
|
|
<div id="result"></div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- JavaScript代码将放在这里 -->
|
|
|
|
|
<script>
|
|
|
|
|
document.getElementById('baseConverterForm').addEventListener('submit', function(event) {
|
|
|
|
|
event.preventDefault(); // 阻止表单默认提交行为
|
|
|
|
|
|
|
|
|
|
const sourceBase = parseInt(document.getElementById('sourceBase').value);
|
|
|
|
|
const targetBase = parseInt(document.getElementById('targetBase').value);
|
|
|
|
|
const valueToConvert = document.getElementById('valueToConvert').value.toUpperCase();
|
|
|
|
|
const valueToConvert = document.getElementById('valueToConvert').value;
|
|
|
|
|
|
|
|
|
|
// 转换逻辑
|
|
|
|
|
let decimalValue = parseInt(valueToConvert, sourceBase);
|
|
|
|
|
let convertedValue = decimalValue.toString(targetBase);
|
|
|
|
|
let decimalValue;
|
|
|
|
|
try {
|
|
|
|
|
decimalValue = parseInt(valueToConvert, sourceBase);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
document.getElementById('result').innerHTML = '请按规范输入!!!';
|
|
|
|
|
return; // 遇到错误时停止执行
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isNaN(decimalValue)) {
|
|
|
|
|
document.getElementById('result').innerHTML = '请按规范输入!!!';
|
|
|
|
|
} else {
|
|
|
|
|
let convertedValue = decimalValue.toString(targetBase);
|
|
|
|
|
// 将结果展示在页面上
|
|
|
|
|
document.getElementById('result').innerHTML = `转换后的值: ${convertedValue}`;
|
|
|
|
|
document.getElementById('result').innerHTML = `转换后的值: <span style="font-family: monospace;">${convertedValue}</span>`;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
</body>
|
|
|
|
|