jQuery 更改进度条“aria-valuenow”属性和 CSS“width”属性的值

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

jQuery to change values of progress-bar "aria-valuenow" attribute and CSS "width" property

jqueryprogress-bar

提问by Ray_Hack

I have a progress bar on my page (Bootstrap) that looks like this:

我的页面(引导程序)上有一个进度条,如下所示:

<div id="theprogressbar" class="progress-bar progress-bar-u" 
     role="progressbar" aria-valuenow="75%" aria-valuemin="0" 
     aria-valuemax="100" style="width: 75%">

I would like to update it via jQuery, I have already done most of the work and calculated the new value I need to put into it, but am unsure of how to target the aria-valuenowand the style="width: "values.

我想通过 jQuery 更新它,我已经完成了大部分工作并计算了我需要放入其中的新值,但不确定如何定位aria-valuenowstyle="width: "值。

Assuming my jQuery gives me the new value+% as a variable called newprogress:

假设我的 jQuery 给了我新的 value+% 作为一个名为的变量newprogress

$('#theprogressbar'). *(please help me finish this line)* (newprogress)

回答by Denis Slonovschi

$('#theprogressbar').attr('aria-valuenow', newprogress).css('width', newprogress);

value default is px if you want set % follow code below

如果你想设置 % 按照下面的代码,默认值为 px

$('#theprogressbar').attr('aria-valuenow', newprogress).css('width', newprogress+'%');

回答by jameshwart lopez

You could use

你可以用

$('#theprogressbar').attr("aria-valuenow","the new value"); 
$('#theprogressbar').attr("style","width:"+theincrementingvalue); 

Or you could use .css in jquery http://api.jquery.com/css/

或者你可以在 jquery http://api.jquery.com/css/ 中使用 .css

回答by Carlo Pescetto

This works better:

这效果更好:

var newprogress=80;
$('#id').width(newprogress + "%").attr('aria-valuenow', newprogress);

回答by learner

To set width use width()method of jQuery.

设置宽度使用width()jQuery的方法。

To access aria-valuenowuse attr().

访问aria-valuenow使用attr().

Combining both:

两者结合:

$('#id').width(newprogress).attr('aria-valuenow', newprogress);

回答by Umesh Sehta

You can apply the attributes of this elements as:

您可以将此元素的属性应用为:

$('#theprogressbar').attr('aria-valuenow',value+'%');   

and 

$('#theprogressbar').attr('width',value+'%');

if the value is in variable.

如果值是可变的。

$('#theprogressbar').attr('aria-valuenow',newprogressvalue);