如何在JS外部文件中简化的javascript中使用双击事件

时间:2017-01-29 19:04:32

标签: javascript events javascript-events event-handling double

我似乎无法弄清楚如何从外部文件清除我的JS代码中的文本框速率,并希望得到一些帮助来弄清楚如何去做。

有人可以帮助我解决代码及其工作原理和方法,以便我可以学习并理解它,以便我可以有效地编写代码。

我在下面列出了所有原始代码。

HTML

<!DOCTYPE html>
<html>
<head>
    <title>Future Value Calculator</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="future_value.css">
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">  
</script>
    <script src="future_value.js"></script>
</head>

<body>
    <section>
        <h1 id="heading">Future Value Calculator</h1>
        <label for="investment">Investment Amount:</label>
        <input type="text" id="investment">
        <span id="investment_error">&nbsp;</span><br>

        <label for="rate">Annual Interest Rate:</label>
        <input type="text" id="rate">
        <span id="rate_error">&nbsp;</span><br>

        <label for="years">Number of Years:</label>
        <input type="text" id="years">
        <span id="years_error">&nbsp;</span><br>
        <label for="future_value">Future Value:</label>
        <input type="text" id="future_value" disabled="disabled"><br>

        <label>&nbsp;</label>
        <input  type="button" id="calculate" value="Calculate"><br>
    </section>
</body>
</html>

的JavaScript

var $ = function (id) {
    return document.getElementById(id);
}

var calculateClick = function () {
    var investment = parseFloat( $("investment").value );
    var annualRate = parseFloat( $("rate").value );
    var years = parseInt( $("years").value );

    if (isNaN(investment) || investment < 100 || investment > 100000) {
        alert("Investment must be an integer from 100 - 100,000.");
    } 
    else if(isNaN(annualRate) || annualRate < .1 || annualRate > 12) {
        alert("Annual rate must be a value from .1 - 12.");
    }
    else if(isNaN(years) || years < 1 || years > 50) {
        alert("Years must be an integer from 1 - 50.");
    }
    // if all entries are valid, calulate future value
    else {
        futureValue = investment;
        for ( i = 1; i <= years; i++ ) {
            futureValue += futureValue * annualRate / 100;
        }
        $("future_value").value = futureValue.toFixed();
    } 
}

window.onload = function () {
    $("calculate").onclick = calculateClick;
    $("investment").focus();
}

rate = document.getElementById("rate");
rate.dblclick = "";

1 个答案:

答案 0 :(得分:0)

添加&#34; ondblclick&#34;属于你的&#34;率&#34; JS中的元素,它的世界看起来像这样:

result = ~(~result & ~mask) ;

然后添加&#34; clearRate()&#34;功能:

$('rate').ondblclick = clearRate();

这意味着当&#34;率&#34;双击元素,它将触发函数&#34; clearRate()&#34;,它设置&#34; rate&#34;的值。一个空字符串。