如何更改某些%

时间:2018-02-23 13:24:07

标签: javascript html css twitter-bootstrap progress-bar

    var elem = document.getElementById("progress-bar");

    var progress = 75;

    elem.style.width = 80 + '%';
    
    
    if (progress > 80) {
      elem.style.background = "red";
    }
#myProgress {
    height: 5px;
}




#progress-bar {
    width: 50%;
    background-color: green;
}
<html>
<body>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript" ></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js" type="text/javascript" ></script>

 <div id="myProgress" class="progress">
       <div id="progress-bar" class="progress-bar progress-bar-striped active" role="progressbar"
                              aria-valuemin="0" aria-valuemax="100">
       </div>
 </div>
 
 </body>
 
 </html>

我正在使用bootstrap进度条,我想知道当百分比高于某个值时是否可以更改此栏的颜色。

我有一个外部Javascript文件,我可以在其中更改条形图进度的属性:

if (progress > 80) {
    elem.style.background = 'red'; // THIS IS WHAT I NEED TO CHANGE
}

elem.style.width = progress + '%';

指令elem.style.background不会改变颜色,有什么建议吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

以下是工作示例:https://jsfiddle.net/0kfpqauq/

elem = document.getElementById("progress-bar");

var progress = 90;

elem.style.width = 80 + '%';


if (progress > 80) {
  elem.style.background = "red";
}