the end version

main
212206253 1 month ago
parent 1ea76fef40
commit 17ef4b703d

@ -2,40 +2,124 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <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> </head>
<body> <body>
<h1>基数转换器</h1> <div class="container">
<form id="baseConverterForm"> <h1>进制转换器</h1>
<label for="sourceBase">源基数 (2-16):</label> <form id="baseConverterForm">
<input type="number" id="sourceBase" name="sourceBase" min="2" max="16" required> <div class="form-group">
<br><br> <label for="sourceBase">初始进制(2-16):</label>
<label for="valueToConvert">要转换的值:</label> <input type="number" id="sourceBase" name="sourceBase" min="2" max="16" required>
<input type="text" id="valueToConvert" name="valueToConvert" required> </div>
<br><br> <div class="form-group">
<label for="targetBase">目标基数 (2-16):</label> <label for="valueToConvert">要转换的值:</label>
<input type="number" id="targetBase" name="targetBase" min="2" max="16" required> <input type="text" id="valueToConvert" name="valueToConvert" required>
<br><br> </div>
<button type="submit">转换</button> <div class="form-group">
</form> <label for="targetBase">目标进制(2-16):</label>
<div id="result"></div> <input type="number" id="targetBase" name="targetBase" min="2" max="16" required>
</div>
<button type="submit">转换</button>
</form>
<div id="result"></div>
</div>
<!-- JavaScript代码将放在这里 -->
<script> <script>
document.getElementById('baseConverterForm').addEventListener('submit', function(event) { document.getElementById('baseConverterForm').addEventListener('submit', function(event) {
event.preventDefault(); // 阻止表单默认提交行为 event.preventDefault(); // 阻止表单默认提交行为
const sourceBase = parseInt(document.getElementById('sourceBase').value); const sourceBase = parseInt(document.getElementById('sourceBase').value);
const targetBase = parseInt(document.getElementById('targetBase').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 decimalValue;
let convertedValue = decimalValue.toString(targetBase); 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> </script>
</body> </body>
</html> </html>
Loading…
Cancel
Save