所以我和一個朋友正在為我的網站構建一個簡單的小公式計算器,我們遇到了一些問題。我希望網頁上的所有按鈕都可以多次運行,而無需重繪 頁面。就目前而言,所有按鈕都可以作業一次,但只有華氏溫度和攝氏溫度按鈕可以多次作業。我完全看不出為什么這些按鈕會重復作業,而其他按鈕卻沒有。這是頁面的代碼。
<!Doctype html>
<html>
<head>
<title>Calculator</title>
<link rel="stylesheet" href="stylesheet1.css">
<script>
function percent() {
numer = prompt("What is the numerator?","<Enter numerator value>");
denom = prompt("What is the denominator?","<Enter denominator value>");
denom2 = prompt("What is the percent denominator?",100)
percent = (numer/denom)* denom2;
alert("The percentage is " percent ".");
}
function fahrenheit() {
cel = prompt("What is the current temperature in degrees Celsuis?","<Enter temperature>");
fah = (cel*9/5) 32;
alert("The temperature in degrees fahrenheit is " fah ".");
}
function celsius() {
fah = prompt("What is the current temperature in degrees fahrenheit?","<Enter temperature>");
cel = (fah-32)*(5/9);
alert("The temperature in degrees celsius is " cel ".");
}
function LPM() {
GPM = prompt("What is the GPM?","<Enter flow rate>");
LPM = (GPM*3.7854117839);
alert("The flow rate is " LPM "Litres per minute.");
}
function GPM() {
LPM = prompt("What is the LPM?","<Enter flow rate>");
GPM = (LPM/3.7854117839);
alert("The flow rate is " GPM "gallons per minute.");
}
</script>
</head>
<body>
<nav>
<ul>
<li>
<a href="task_handler.php"> Task Handler </a>
</li>
<li>
<a href="index.php">Home</a>
</li>
<li>
<a href="grocery_list.php"> Grocery List </a>
</li>
<li>
<a href="3d_printers.php"> 3D Printer HUB </a>
</li>
<li>
<a href="store.php"> Store </a>
</li>
<li>
<a href="about.php"> About </a>
</li>
</ul>
</nav>
<div class="socials">
<ul>
<li>
<a href="https://www.youtube.com/channel/UCbScAAcSVrM1JRjN3ougp8w" target="_blank" rel="noopener noreferrer"> YouTube </a>
</li>
<li>
<a href="https://github.com/AlbertaKoolKid" target="_blank" rel="noopener noreferrer"> GitHub </a>
</li>
</ul>
</div>
<h1>Useful Calculator</h1>
<div>
<h3>Celsius/Fahrenheit</h3>
<p> This will convert between Celsius and Fahrenheit. Select the unit you would like to convert to. </p>
<button type="button" onclick="fahrenheit()">Fahrenheit</button>
<button type="button" onclick="celsius()">Celsius</button>
</div>
<div>
<h3>Percent Calculator</h3>
<p> Enter the numerator and denominator to calculate percent. </p>
<button type="button" onclick="percent()">Percentage</button>
</div>
<h3>Flow Rate</h3>
<p> Use this function to calculate flow rate in Gallons per minute(GPM) or litres per minute.(LPM).
<button type="button" onclick="GPM()">GPM</button>
<button type="button" onclick="LPM()">LPM</button>
</body
</html>
任何幫助或想法將不勝感激。
uj5u.com熱心網友回復:
您的變數與您的函式名同名。因此,您可以呼叫這些函式一次,但是當它執行時,函式的名稱將不復存在,因為現在需要一個具有該名稱的變數。第二次按下按鈕并想要呼叫具有該名稱的函式時,它不再存在,因為 JS 現在只使用該變數(因為那是具有該名稱的最后一件事)。
嘗試混合不同的名稱或大小寫,例如:
函式 percent() -> 可變百分比
函式 GPM() -> 變數 gpm
函式 LPM() -> 變數 lpm
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/416453.html
標籤:
