jQuery 通过单击按钮增加数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7183226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:30:48 来源:igfitidea点击:
Increment a number by clicking onto a button
提问by Anon
I have the number $likes_number
in a div block on my page. I would like to know how to increment it dynamically by clicking on a button?
我的页面上有$likes_number
一个 div 块中的数字。我想知道如何通过单击按钮动态增加它?
My code:
我的代码:
<div id="likes">
<span class="figure">
<?php echo $likes_number ;?>
</span>
</div>
<button type="button" id="like" >Like</button>
回答by CONvid19
<script type="text/javascript" src="//code.jquery.com/jquery-1.6.2.js"></script>
<div id="likes">
<span class="figure"></span>
</div>
<button type="button" id="like">Like</button>
<script type="text/javascript">
var clicks = 0; $("#like").click(function(){ clicks++; $('.figure').html(clicks);});
</script>
Live example: https://jsfiddle.net/nTmu7/2/
回答by Bobby Hyman
$('#like').click(function() {
$('#likes span').text( parseInt($('#likes span').text()) + 1 );
});
You can see an example here: http://jsfiddle.net/nayish/GCvnH/
你可以在这里看到一个例子:http: //jsfiddle.net/nayish/GCvnH/