简体   繁体   English

动态进度条Javascript和HTML

[英]Dynamic progress bar Javascript and HTML

I am trying to create a dynamic bar in HTML using javascript. 我正在尝试使用javascript在HTML中创建动态栏。 I have create the button but cannot seem to pass the value over to the progress bar. 我已创建按钮但似乎无法将值传递到进度条。 Can someone please help me? 有人可以帮帮我吗? thanks! 谢谢!

<button onclick="increase()">Add</button> 
<button onclick="decrease()">Minus</button> 
<input type="text" id="tb"> 
<script type="text/javascript"> 
var value = 0 document.getElementById("tb").value = value; 
function increase(){ 
  this.value = value + 1; document.getElementById("tb").value=value;     
} 
function decrease(){ 
  this.value = value - 1; document.getElementById("tb").value=value; 
} 
document.write("<div class='meter'><span style='width: 30%'></span> </div>");
document.write("<input type='text' id=\"tb\">"+value +" </input>"); 
</script>

It'd be easier to do this in jQuery, but here it goes with POJS: 在jQuery中执行此操作会更容易,但在这里它与POJS一起使用:

js: JS:

var value = 0, 
tb = document.getElementById("tb"),
progress = document.getElementById("progress"); //store these, it's better
function increase(){ 
  value++;// same as value += 1, but better
  if(value>=100) value = 100;//keep it under 100%
  tb.value = value;// set the value of the text field     
  progress.style.width = value + "%";// set the width of the progress bar
} 
function decrease(){ 
  value--;
  if(value<=0) value = 0;//keep it over 0%
  tb.value = value; 
  progress.style.width = value + "%";
} 

document.write is janky, so I ditched that & put the bar in the markup. document.write是janky,所以我放弃了并将标记放入标记中。

html: HTML:

<button onclick="increase()">Add</button> 
<button onclick="decrease()">Minus</button> 
<input type="text" id="tb"> 
<div id='meter'><div id='progress'></div></div>

css: CSS:

​#meter {border:1px solid #000;width:100px}
#progress {background:#333;height:10px;width:0%}​

fiddle: http://jsfiddle.net/sw95b/ 小提琴: http//jsfiddle.net/sw95b/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM