99 lines
3.4 KiB
HTML
99 lines
3.4 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>电容/电阻值转换</title>
|
|
</head>
|
|
|
|
<body>
|
|
<h1>电容/电阻值转换工具</h1>
|
|
<label for="typeSelect">选择类型:</label>
|
|
<select id="typeSelect">
|
|
<option value="capacitance">电容</option>
|
|
<option value="resistance">电阻</option>
|
|
</select>
|
|
<br><br>
|
|
<label for="valueInput">输入值:</label>
|
|
<input type="text" id="valueInput" placeholder="输入电容/电阻值" onkeydown="handleKeyDown(event)">
|
|
<button onclick="convertValue()">转换</button>
|
|
<p id="output">转换结果将在这里显示</p>
|
|
|
|
<script>
|
|
function cap_namer(value_str) {
|
|
value_str = value_str.toLowerCase();
|
|
if (value_str.includes('u')) {
|
|
base = 1000000;
|
|
} else if (value_str.includes('n')) {
|
|
base = 1000;
|
|
} else if (value_str.includes('p')) {
|
|
base = 1;
|
|
} else {
|
|
throw '错误的值';
|
|
}
|
|
value_flt = parseFloat(value_str);
|
|
inner_flt = value_flt * base;
|
|
if (inner_flt < 10) {
|
|
code = inner_flt.toString().replace('.', '');
|
|
} else {
|
|
code = inner_flt.toString().slice(0, 2) + (inner_flt.toString().length - 4);
|
|
}
|
|
return code;
|
|
}
|
|
|
|
function res_namer(value_str) {
|
|
value_str = value_str.toLowerCase();
|
|
if (value_str.includes('k')) {
|
|
base = 1000;
|
|
} else if (value_str.includes('m')) {
|
|
base = 1000000;
|
|
} else if (value_str.includes('g')) {
|
|
base = 1000000000;
|
|
} else if (value_str.includes('r')) {
|
|
base = 1;
|
|
} else {
|
|
throw '错误的值';
|
|
}
|
|
value_flt = parseFloat(value_str);
|
|
inner_flt = value_flt * base;
|
|
if (inner_flt < 1 && inner_flt > 0) {
|
|
inner_flt=inner_flt*10
|
|
code = "0"+"R"+inner_flt.toString().replace('.', '');
|
|
}
|
|
else if (inner_flt < 10 && inner_flt > 1) {
|
|
code = inner_flt.toString().replace('.', '');
|
|
} else {
|
|
code = inner_flt.toString().slice(0, 2) + (inner_flt.toString().length - 2);
|
|
}
|
|
return code;
|
|
}
|
|
|
|
function convertValue() {
|
|
const typeSelect = document.getElementById("typeSelect");
|
|
const valueInput = document.getElementById("valueInput");
|
|
const outputElement = document.getElementById("output");
|
|
const valueType = typeSelect.value;
|
|
const inputValue = valueInput.value;
|
|
|
|
try {
|
|
let convertedValue;
|
|
if (valueType === "capacitance") {
|
|
convertedValue = "C" + cap_namer(inputValue);
|
|
} else if (valueType === "resistance") {
|
|
convertedValue = "R" + res_namer(inputValue);
|
|
}
|
|
outputElement.textContent = "转换结果:" + convertedValue;
|
|
} catch (error) {
|
|
outputElement.textContent = "错误:" + error;
|
|
}
|
|
}
|
|
|
|
function handleKeyDown(event) {
|
|
if (event.key === "Enter") {
|
|
convertValue();
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
|
|
</html> |