[英]How to display a counter output to text rather than an input
我创建了一个基本的JavaScript计数器,该计数器计算按钮被按下的次数,然后将其显示在输入框中。
我要更改它,以便它计算按下的次数并将其显示为纯文本。
例如5,123
HTML:
<div class="col-md-2 col-md-offset-3">
<div class="outer"><img src="assets/img/f1.jpg" alt="..." class="img-rounded" style="width:250px;height:150px"></div>
<div class="inner">
<center><button class="btn btn-default" value = "Click" onclick = "count()">Vote</button></center>
<input id = "counting" type = "text" class="votecount" />
</div>
</div>
CSS:
.outer {
position:absolute;
}
.inner {
position:relative;
margin-top:50px;
}
.votecount {
width:50px;
}
JAVASCRIPT:
<script type ="text/javascript">
var x = 0;
function count()
{
x += 1;
document.getElementById( "counting" ).value = x;
}
</script>
使用类似的方法..首先获得计数的值..如果没有表决..在其中添加1 ..如果已经有表决..将其递增
function count()
{
var x = document.getElementById("counting").value;
if(x==""){
document.getElementById( "counting" ).value = 1;
}else{
document.getElementById( "counting" ).value = parseInt(x)+1;
}
}
这是小提琴的链接http://jsfiddle.net/szWth/
更改:
<input id = "counting" type = "text" class="votecount" />
至:
<span id = "counting" class="votecount"></span>
使输入成为跨度,并更改DOM查询以更改“ innerHTML”: function count() { x += 1; document.getElementById( "counting" ).innerHTML = x; }
function count() { x += 1; document.getElementById( "counting" ).innerHTML = x; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.