动态进度条 Javascript 和 HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9727508/
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-10-26 07:38:02  来源:igfitidea点击:

Dynamic progress bar Javascript and HTML

javascripthtml

提问by Brian Li

I am trying to create a dynamic bar in HTML using javascript. I have create the button but cannot seem to pass the value over to the progress bar. Can someone please help me? thanks!

我正在尝试使用 javascript 在 HTML 中创建一个动态栏。我已经创建了按钮,但似乎无法将值传递给进度条。有人可以帮帮我吗?谢谢!

<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>

回答by JKirchartz

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.writeis janky, so I ditched that & put the bar in the markup.

document.write很笨拙,所以我放弃了它并将该栏放在标记中。

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/