我需要这个简单的百分比web应用程序的帮助

I need help on this simple percentage web app

本文关键字:百分比 web 应用程序 帮助 简单      更新时间:2023-09-26

我需要帮助!当您按下每个按钮时,它应该在body中附加该按钮的百分比。由于某些原因,它似乎没有显示它。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
    var numOfPressedOne = 0;
    var numOfPressedTwo = 0;
    var totalPressed = 0;
    var buttonOnePct = numberOfPressedOne/totalPressed;
    var buttonTwoPct = 0;
    var pressedOne = function(){
        numberOfPressedOne ++;
        totalPressed ++;
        $("body").append(buttonOnePct + "%");       
    };
    var pressedTwo = function(){
        numberOfPressedTwo ++;
        totalPressed ++;
        $("body").append(buttonTwoPct + "%");       
    };
</script>
</head>
<body>
    <input type="button" id="buttonOne" value="press" onclick="pressedOne;"/>
    <input type="button" id="buttonTwo" value="press" onclick="pressedTwo;"/>
</body>

您的代码有很多问题。以下是我的看法:

<script>
    var numberOfPressedOne = 0;
    var numberOfPressedTwo = 0;
    var totalPressed = 0;
    var buttonOnePct = numberOfPressedOne/totalPressed;
    var buttonTwoPct = 0;
    var pressedOne = function(){
        numberOfPressedOne ++;
        totalPressed ++;
        buttonOnePct = numberOfPressedOne/totalPressed;
        $("body").append("<br'>button 2: " + buttonOnePct*100 + "%");
    };
    var pressedTwo = function(){
        numberOfPressedTwo ++;
        totalPressed ++;
        buttonTwoPct = numberOfPressedTwo/totalPressed
        $("body").append("<br'>button 1: " + buttonTwoPct*100 + "%");
    };
</script>
</head>
<body>
    <input type="button" id="buttonOne" value="press" onclick="pressedOne();"/>
    <input type="button" id="buttonTwo" value="press" onclick="pressedTwo();"/>
</body>

首先,您使用了名为numberOfPressedOnenumOfPressedOne的变量

其次,buttonOnePctbuttonTwoPct需要在每次用户点击

时更新。

第三,按钮需要是onclick="pressedOne();"而不是onclick="pressedOne;"

最后,我添加了一些代码来美化输出,使其为"<br'>button 1: " + buttonTwoPct*100 + "%",但你可以恢复这个,如果你想。

问题是你引用了你的函数,但你没有调用它们。试试这样做:

<input type="button" id="buttonOne" value="press" onclick="pressedOne()" />

EDIT:您的函数也没有创建任何新元素。试试以下命令:

var pressedOne = function() {
    numberOfPressedOne ++;
    totalPressed ++;
    $("body").append("<p>" + buttonOnePct + "%</p>");
};