? ? ? ?作為程序員,沒有合適的工具溉箕,就得手搓一個晦墙,PC端,移動端均可適用肴茄。廢話不多說晌畅,直接上代碼。
HTML:
<div class="calculator"> <label for="inputValue">輸入金額:</label> <input id="inputValue" step="0.01" type="number" placeholder="請輸入金額"> <label for="inputUnit">選擇輸入單位:</label> <select id="inputUnit"> <option value="cny">人民幣 (CNY)</option> <option value="jiao">角 (角)</option> <option value="fen">分 (分)</option> </select> <label for="outputUnit">選擇輸出單位:</label> <select id="outputUnit"> <option value="cny">人民幣 (CNY)</option> <option value="jiao">角 (角)</option> <option value="fen">分 (分)</option> </select> <button onclick="convertRMB()">計(jì)算</button> <div class="result"><p>轉(zhuǎn)換結(jié)果:</p><p id="outputValue">0</p></div> </div>
JS:
function convertRMB() { const inputValue = parseFloat(document.getElementById('inputValue').value); const inputUnit = document.getElementById('inputUnit').value; const outputUnit = document.getElementById('outputUnit').value; if (isNaN(inputValue) || inputValue < 0) { alert('請輸入有效的金額'); return; } const conversionRates = { cny: 1, jiao: 0.1, fen: 0.01 }; const valueInFen = inputValue / conversionRates[inputUnit]; const convertedValue = valueInFen * conversionRates[outputUnit]; document.getElementById('outputValue').textContent = convertedValue.toFixed(2); }
CSS:
.calculator { width: 300px; background-color: #333; color: white; padding: 20px; border-radius: 10px; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3); } label { display: block; margin-bottom: 10px; font-size: 16px; } input, select { width: 100%!important; padding: 10px!important; margin-bottom: 20px; color: #000000; border-radius: 5px; border: 1px solid #555; font-size: 16px!important; background-color: #ffffff!important; } button { width: 100%; padding: 10px; background-color: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; } button:hover { background-color: orange; } .result { margin-top: 20px; text-align: center; } option { background-color: #ffffff; } p { font-size: 18px; margin-top: 5px!important; }
?線上運(yùn)行寡痰,可以直接打開:人民幣單位換算計(jì)算器