JavaScript结果未显示在PhoneGap中

时间:2018-09-02 11:12:12

标签: javascript phonegap

我正在学习使用PhoneGap构建应用程序,并且已经制作了计算器,但是单击“计算”后结果不会显示。请查看代码。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
        <title>Dog Years Calculator</title>
        <script>
            windows.onload=function{
                    document.getElementById('btnCalc').addEventListener('click', calcAge);
                }       
                function calcAge(e)
                {
                    var age = document.getElementById('dogAge').value;
                    age = age * 7;
                    var result = "In Dog years, your dogs age is: " + age;
                    document.getElementById('result').innerHTML = result;
                }
        </script>
    </head>
    <body>
        <script type="text/javascript" src="cordova.js"></script>
        <h1>Dog Years Calculator</h1>
        <label for="dogAge">Dogs age in Human years:</label>
        <input id="dogAge" type="number" />
        <button id="btnCalc">Calculate</button>
        <div id="result"></div>
    </body>

</html>

1 个答案:

答案 0 :(得分:0)

像这样使用:

 btnCalc.onclick = function(){
                    var age = document.getElementById('dogAge').value;
                    age = age * 7;
                    var result = "In Dog years, your dogs age is: " + age;
                    document.getElementById('result').innerHTML = result;
                };
        <h1>Dog Years Calculator</h1>
        <label for="dogAge">Dogs age in Human years:</label>
        <input id="dogAge" type="number" />
        <button id="btnCalc">Calculate</button>
        <div id="result"></div>