Javascript进度条按钮,可将值增加1并将样式宽度更改10

时间:2019-03-13 14:45:23

标签: javascript bootstrap-4

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("Download Finished");
	}
}

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,但我还希望宽度样式每次增加10。我想知道是否有人可以帮助我。谢谢。

以下是Javascript部分

function getProgress() {
    return document.getElementById("progressbar").getAttribute("aria-valuenow");
    document.getElementById("progressbar").getAttribute("style","width");
    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("Download Finished");
    }
}

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

function resetButton() {
    var r = getProgress();
    setProgress(r = 0);
}

这是项目的HTML部分。

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

可以根据需要提供更多详细信息,谢谢

1 个答案:

答案 0 :(得分:0)

您的getProgress和setProgress函数存在问题。在getProgress中,您要返回3次,这不是return的工作方式,在这种情况下,我们将获取值,仅返回值。在setProgress中,样式/宽度的setAttribute在语法上不正确。现在可以使用的版本如下。

function getProgress() {
  var d = document.getElementById("progressbar").getAttribute("aria-valuenow");
  return d;
}

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("Download Finished");
  }
}

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>