简体   繁体   English

多个具有不同值的元素的JS数据属性

[英]JS data-attribute for multiple elements with different values

I code that shows a progress bar: 我编码显示进度条:

<span class="starsCount" data-length="64"></span>

<span class="starsCount" data-length="13"></span>

<span class="starsCount" data-length="33"></span>

and assigned bar length using Jquery UI Progressbar Widget by calling JS function in the way: 并通过调用JS函数的方式使用Jquery UI Progressbar Widget分配条长:

$(function () {
    $(".starsCount").progressbar({
        value: $('.starsCount').data('length')
    });
});

Now, I got an issue that all elements have the equal bar length of the first one data-length="64" . 现在,我遇到一个问题,即所有元素的条长与第一个data-length="64"相等。 Any help is much appreciated. 任何帮助深表感谢。

在此处输入图片说明

Code snippet: http://jsfiddle.net/hbLw34ec/ 代码段: http : //jsfiddle.net/hbLw34ec/

You need to loop over each specific element and instantiate the progressbar on them individually. 您需要遍历每个特定元素,并分别实例化它们上的progressbar This allows you to access the data attribute of each element in the set using the this keyword. 这使您可以使用this关键字访问集合中每个元素的data属性。 Try this: 尝试这个:

$(".starsCount").each(function() {
    $(this).progressbar({
        value: $(this).data('length')
    });
});

Example fiddle 小提琴的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM