我想删除 HTML 文档中的 onblur 和名称 (this.form),这样当用户单击按钮时,它只会调用该函数

发布于 2024-11-06 05:33:58 字数 2538 浏览 0 评论 0原文

<!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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那片花海 2024-11-13 05:33:58

由于您的 ConvertCurrency 函数不需要任何参数,因此您甚至不需要 this.form 参数。另外,您可以删除blur=...来摆脱onblur事件。如果您也想摆脱它,那么 select 的 onchange 事件也是如此。

更新:愚蠢的我复制粘贴......

<!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");
                var cmbCurrency = document.getElementById("cmbCurrency");

                //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 i = cmbCurrency.selectedIndex;
                var strCurrency = cmbCurrency.options[i].text;
                txtConversion.value = (txtSterling.value * dblExchangeRate[i]).toFixed(2) + ' ' + strCurrency;
            }
        </script>
    </head>
    <body>
        <p>Currency Converter</p>
        <form action="">
            £ <input id="txtSterling" type="text" name="txtSterling">
            <select id="cmbCurrency" name="cmbCurrency" onchange="convertCurrency();">
                <option>Yen</option>
                <option>US Dollars</option>
                <option>Euro</option>
                <option>Swiss Frank</option>
                <option>Danish Korona</option>
            </select>
            <input type="button" id="cmdCurrency" value="Convert" name="cmdCurrency" onclick="convertCurrency();">
            <input type="text" id="txtConversion" name="txtConversion" readonly="readonly">
        </form>
    </body>
</html>

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...

<!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");
                var cmbCurrency = document.getElementById("cmbCurrency");

                //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 i = cmbCurrency.selectedIndex;
                var strCurrency = cmbCurrency.options[i].text;
                txtConversion.value = (txtSterling.value * dblExchangeRate[i]).toFixed(2) + ' ' + strCurrency;
            }
        </script>
    </head>
    <body>
        <p>Currency Converter</p>
        <form action="">
            £ <input id="txtSterling" type="text" name="txtSterling">
            <select id="cmbCurrency" name="cmbCurrency" onchange="convertCurrency();">
                <option>Yen</option>
                <option>US Dollars</option>
                <option>Euro</option>
                <option>Swiss Frank</option>
                <option>Danish Korona</option>
            </select>
            <input type="button" id="cmdCurrency" value="Convert" name="cmdCurrency" onclick="convertCurrency();">
            <input type="text" id="txtConversion" name="txtConversion" readonly="readonly">
        </form>
    </body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文