我需要这个简单的百分比web应用程序的帮助
I need help on this simple percentage web app
我需要帮助!当您按下每个按钮时,它应该在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>
首先,您使用了名为numberOfPressedOne
和numOfPressedOne
的变量
其次,buttonOnePct
和buttonTwoPct
需要在每次用户点击
第三,按钮需要是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>");
};
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- 在Web应用程序中使用Highcharts javascript
- WebDriverException:tinyMCE未在selenium Web driver java中定义
- Selenium Web驱动程序和javascript
- Windows形成web浏览器控件和Javascript更改的DOM
- 如何将javascript事件从web浏览器wpf控件发送到wpf的c#代码
- Steam Web API:I'我不知道该如何处理这个看起来像哈希的url来生成图像
- 在调用speak之前预加载Web Speech API
- 如何在visualstudio中调试web api时编辑javascript文件
- 是否存在React Native“;WEB代码安全防护”;
- 如何在Ionic Android中将Javascript注入到web视图中
- 将复杂对象从angular js传递到web api,它总是返回404
- 如果宽度是百分比,如何设置以px为单位的图像高度
- 基于api密钥的NodeJS web服务
- 将下载链接从web浏览器传递给第三方应用程序
- 使用javascript可以设置css动画的当前百分比
- 基于Web的JS调试库
- 使用javascript在客户端上使用Web服务
- 如何通过安全的https连接在javascript中使用基于soap xml的Web服务
- 我需要这个简单的百分比web应用程序的帮助