Javascript Jquery没有正确循环函数
我是开发jquery插件的新手,我一直坚持在函数调用中使用我的函数,在元素中重复相同的值。我确实希望分别替换我页面的所有值Javascript Jquery没有正确循环函数,javascript,jquery,jquery-plugins,Javascript,Jquery,Jquery Plugins,我是开发jquery插件的新手,我一直坚持在函数调用中使用我的函数,在元素中重复相同的值。我确实希望分别替换我页面的所有值 ( function ($) { $.fn.siPrifixx = function (value, options) { // This is the easiest way to have default options. var settings = $.extend({ // These
( 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));
我的代码怎么了?当调用
$('.widget data').siPrifixx时,您仍然使用widget data
类对所有元素进行寻址。因为您已经在迭代该集合,所以不应该在每次迭代中都以所有元素为目标。而是调用$(this).siPrifixx(…)代码>您可以尝试使用以下代码:
$(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
});
});
什么是data.stats
?这是我的ajax响应数据集。我的数据没有问题,data.stats[index];不会更改,我将$('.widget data').siPrifixx更改为$(此)。siPrifixx。感谢您的贡献!
$(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
});
});