我想删除 HTML 文档中的 onblur 和名称 (this.form),这样当用户单击按钮时,它只会调用该函数
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
//Create a global an global awaay for strCurrency & dblExchange Rate
var strCurrency=['Yen','US Dollar','Euro','Swiss Frank','Danish Korona'];
var dblExchangeRate=[128.7, 1.59, 1.15, 2.06, 9.69];
//Declare the function convert currency
function convertCurrency()
{
var txtSterling= document.getElementById("txtSterling");
var selCurrency= document.getElementById("cmbCurrency");
var txtConversion= document.getElementById ("txtConversion");
//reset to no value
txtConversion.value='';
//validate for numbers
if(isNaN(txtSterling.value))
{
txtSterling.value='';
alert('Please insert a numerical value');
txtSterling.focus();
return;
}
var strCurrency=selCurrency = document.getElementById("cmbCurrency").selectedIndex;
txtConversion.value = (txtSterling.value * dblExchangeRate[i]).toFixed(2) + ' ' + strCurrency[i];
}
</script>
</head>
<body>
<p>Currency Converter</p>
<form action="">
£ <input type="text" name="txtSterling" onblur="convertCurrency(this.form)">
<select name="cmbCurrency" onchange="convertCurrency(this.form)">
<option>Yen</option>
<option>US Dollars</option>
<option>Euro</option>
<option>Swiss Frank</option>
<option>Danish Korona</option>
</select>
<input type="button" value="Convert" name="cmdCurrency" onclick="convertCurrency(this.form)">
<input type="text" name="txtConversion" readonly="readonly">
</form>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
//Create a global an global awaay for strCurrency & dblExchange Rate
var strCurrency=['Yen','US Dollar','Euro','Swiss Frank','Danish Korona'];
var dblExchangeRate=[128.7, 1.59, 1.15, 2.06, 9.69];
//Declare the function convert currency
function convertCurrency()
{
var txtSterling= document.getElementById("txtSterling");
var selCurrency= document.getElementById("cmbCurrency");
var txtConversion= document.getElementById ("txtConversion");
//reset to no value
txtConversion.value='';
//validate for numbers
if(isNaN(txtSterling.value))
{
txtSterling.value='';
alert('Please insert a numerical value');
txtSterling.focus();
return;
}
var strCurrency=selCurrency = document.getElementById("cmbCurrency").selectedIndex;
txtConversion.value = (txtSterling.value * dblExchangeRate[i]).toFixed(2) + ' ' + strCurrency[i];
}
</script>
</head>
<body>
<p>Currency Converter</p>
<form action="">
£ <input type="text" name="txtSterling" onblur="convertCurrency(this.form)">
<select name="cmbCurrency" onchange="convertCurrency(this.form)">
<option>Yen</option>
<option>US Dollars</option>
<option>Euro</option>
<option>Swiss Frank</option>
<option>Danish Korona</option>
</select>
<input type="button" value="Convert" name="cmdCurrency" onclick="convertCurrency(this.form)">
<input type="text" name="txtConversion" readonly="readonly">
</form>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您的 ConvertCurrency 函数不需要任何参数,因此您甚至不需要 this.form 参数。另外,您可以删除blur=...来摆脱onblur事件。如果您也想摆脱它,那么 select 的 onchange 事件也是如此。
更新:愚蠢的我复制粘贴......
Since your convertCurrency function doesn't require any arguments, you don't even need the this.form parameter. Also, you can just remove the blur=... to get rid of the onblur event. The same goes for the select's onchange event, if you wanted to get rid of it as well.
UPDATED: Silly me for copy pasting...