Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery UI ProgressBar-设置值等于HTML属性_Jquery_Html_Jquery Ui - Fatal编程技术网

jQuery UI ProgressBar-设置值等于HTML属性

jQuery UI ProgressBar-设置值等于HTML属性,jquery,html,jquery-ui,Jquery,Html,Jquery Ui,我试图在同一个类下,用一个jQuery片段初始化多个Progressbars。我希望将该值设置为HTML属性“value” 以下是html: <div class="pop" value="98"></div> <div class="pop" value="85"></div> <div class="pop" value="78"></div> <div class="pop" value="54"><

我试图在同一个类下,用一个jQuery片段初始化多个Progressbars。我希望将该值设置为HTML属性“value”

以下是html:

<div class="pop" value="98"></div>
<div class="pop" value="85"></div>
<div class="pop" value="78"></div>
<div class="pop" value="54"></div>
但事实并非如此。进度条会出现,但看起来好像它们的值是0

如果我将其更改为:

$( ".pop" ).progressbar({
value: parseInt($(".pop").attr("value"))
});
然后它们都被很好地初始化,但是它们的值都被设置为第一个HTML元素的值(在本例中,它们都被设置为98)。我预料到了


那么还有什么方法可以做到这一点呢?

您想使用jQuery.each函数。下面是一个例子:

$.each($(".pop"), function() {
    $(this).progressbar({ value: parseInt($(this).attr("value")) });
});
另一个例子:

$(".pop").each(function() {
    $(this).progressbar({ value: parseInt($(this).attr("value")) });
});

您希望使用jQuery.each函数。下面是一个例子:

$.each($(".pop"), function() {
    $(this).progressbar({ value: parseInt($(this).attr("value")) });
});
另一个例子:

$(".pop").each(function() {
    $(this).progressbar({ value: parseInt($(this).attr("value")) });
});

我将循环遍历每个div,以确保它获取了正确的“value”属性,因为第一个示例中的
$(this)
没有提供任何信息,因为
this
是该上下文中的窗口对象

$('.pop').each(function(index) {
    $(this).progressbar({
        value: parseInt($(this).attr("value"))
    });      
});

我将循环遍历每个div,以确保它获取了正确的“value”属性,因为第一个示例中的
$(this)
没有提供任何信息,因为
this
是该上下文中的窗口对象

$('.pop').each(function(index) {
    $(this).progressbar({
        value: parseInt($(this).attr("value"))
    });      
});

必须设置值属性:

var progressBar = $('#progressBar');
var v = 70;
progressBar.arrt('value', v);

必须设置值属性:

var progressBar = $('#progressBar');
var v = 70;
progressBar.arrt('value', v);

谢谢你和我上面的答案!谢谢你和我上面的答案!非常感谢。我可以问一下为什么$this是窗口而不是元素吗?progressbar方法没有设置“this”值,这意味着它将默认为当前范围内的任何对象,通常是窗口对象。使用each()将其设置为循环所在的当前元素/对象。谢谢!我可以问一下为什么$this是窗口而不是元素吗?progressbar方法没有设置“this”值,这意味着它将默认为当前范围内的任何对象,通常是窗口对象。使用each()将其设置为循环所在的当前元素/对象。