gpt4 book ai didi

javascript - 动态进度条 Javascript 和 HTML

转载 作者:行者123 更新时间:2023-11-30 13:22:29 24 4
gpt4 key购买 nike

我正在尝试使用 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>

最佳答案

在 jQuery 中这样做会更容易,但这里使用 POJS:

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 是简陋的,所以我放弃了它并将栏放在标记中。

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:

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

fiddle :http://jsfiddle.net/sw95b/

关于javascript - 动态进度条 Javascript 和 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9727508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com