Jquery没有正确循环函数

时间:2016-08-23 06:02:42

标签: javascript jquery jquery-plugins

我是开发jquery插件的新手,我在函数调用中遇到了我的函数我的插件在元素上重复相同的值。我希望分别替换我页面的所有值

    ( function ($) {

    $.fn.siPrifixx = function (value, options) {


        // This is the easiest way to have default options.
        var settings = $.extend({
            // These are the defaults.
            maxDigits: 8,
            seperator: true,
            decimal: 1,
            popUp: true,
            index: "tool tip message"
        }, options);

        console.log(settings.index);

        $(this).addClass('tooltip', 'test');
        $(this).tooltipster({
            theme: 'tooltipster-default',
            functionInit: function () {
                return value
            }
        })

        // $('.tooltip').prop(settings.index, value);

        var number = value;

        if (typeof value === 'string') {
            var parts = value.split(",");
            number = (parseInt(parts.join("")));
        }




            if (typeof number !== 'undefined' && !isNaN(number)) {

                // if the number is alreadey comma seperated convert to number
                var n = settings.decimal
                // 2 decimal places => 100, 3 => 1000, etc
                var decPlace = Math.pow(10, n);

                // Enumerate number abbreviations
                var abbrev = ["K", "M", "B", "T"];
                // Go through the array backwards, so we do the largest first
                for (var i = abbrev.length - 1; i >= 0; i--) {

                    // Convert array index to "1000", "1000000", etc
                    var size = Math.pow(10, (i + 1) * 3);

                    // If the number is bigger or equal do the abbreviation
                    if (size <= number) {
                        // Here, we multiply by decPlaces, round, and then divide by decPlaces.
                        // This gives us nice rounding to a particular decimal place.
                        number = Math.round(number * decPlace / size) / decPlace;

                        // Handle special case where we round up to the next abbreviation
                        if ((number == 1000) && (i < abbrev.length - 1)) {
                            number = 1;
                            i++;
                        }

                        // Add the letter for the abbreviation
                        number += abbrev[i];

                        // We are done... stop

                        break;
                    }


                }

                $(this).html(number)
                console.log(number)
                // return number;
            } else {
                $(this).html(number)
                console.log(number)
                // return value;
            }

    };

}(jQuery));

我在这样的循环上调用函数。

$.each($(plugin.element).find('.widget-data'), function(index, value)
{
    var index = $(this).data('index');
    var value = data.stats[index];
    $('.widget-data').siPrifixx(value,{
            decimal:2,
            index:index
    });

我的代码出了什么问题?

2 个答案:

答案 0 :(得分:1)

当您致电$('.widget-data').siPrifixx时,您仍然使用widget-data课程处理所有元素。由于您已经在迭代该集合,因此您不应该在每次迭代中定位所有元素。而是致电$(this).siPrifixx(...);

答案 1 :(得分:1)

您可以尝试使用以下代码:

$(plugin.element).find('.widget-data').each(function(index)
{
    var index_current = $(this).data('index');
    var value = data.stats[index_current];
    $(this).siPrifixx(value,{
            decimal:2,
            index:index_current
    });
});