单击增量按钮时,进度条的宽度将发生变化。 Java脚本

时间:2019-03-13 18:49:40

标签: javascript

现在工作

好的,您好,我正在寻找以下代码的帮助:

当我单击增量按钮时,每次单击时值将递增一,这实际上是我想要执行的操作。但是我也希望蓝色进度条随着数字的增加而填充。 我有一段可运行的代码,可能有助于在下面进行调试。  感谢所有帮助,因为我是这个网站的新手。谢谢。

function getProgress() {
	return document.getElementById("progressbar").getAttribute("aria-valuenow");
	return document.getElementById("progressbar").getAttribute("style","width");
	return document.getElementById("progressbar").innerHTML;
	}

function setProgress(value) {
	document.getElementById("progressbar").setAttribute("aria-valuenow",value);
	document.getElementById("progressbar").setAttribute("style","width: " +value+ "%");	
	document.getElementById("progressbar").innerHTML = (value+ "%"); 
}

function increment() {
	var i = getProgress();
	if(i < 100){
		i++;
		setProgress(i);	
	}else{
		alert("Progress Complete!");
	}
}

function decrement() {
	var d = getProgress();
	setProgress(d - 1);
}

function resetButton() {
	var r = getProgress();
	setProgress(r = 0);
}
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
	
		
    <title>Progress Bar</title>
	
  </head>
  <body>
	<!-- Container -->
	<div class="container">
	
    <h1>This Process bar is animated using <br>JavaScript!</h1>
	<br>
	
	<!-- Div For Progress Bar -->
	<div class="progress">
		<div class="progress-bar progress-bar-striped" role="progressbar"  style="width: 0%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" id="progressbar" >0%</div>
	</div>
	
	<br>
	<!-- Buttons -->
	<button type="button" class="btn btn-primary" onclick = "increment()">Increment</button>
	<button type="button" class="btn btn-dark" onclick="resetButton()">Reset</button>
	<button type="button" class="btn btn-success" onclick="decrement()">Decrement</button>
	<button type="button" class="btn btn-warning" onclick="">Start Auto Progress!</button>
	<button type="button" class="btn btn-danger" onclick="">Stop Auto Progress!</button>
	
	<p id="value"> </p>
	<!-- End of Container -->
	</div>
	
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
	<script src="Assignment4.js"></script>
 </body>
</html>

1 个答案:

答案 0 :(得分:0)

您只需要在行中缺少单个冒号来设置宽度。应该改为"width: " + value