jQuery 工具提示设置循环中的动态内容正在返回最后一个值
jQuery tooltip set dynamic content in loop is returning last value
变量 intcontent
对于所有工具提示元素始终是相同的值。如何将正确的值传递给工具提示内容函数?
for(var x=0; x<arr_dj[i].style.length; x++){
if(x != 0)$divstyle.append($('<span> / </span>'));
var $span = $('<span></span>');
$span.append(getconststr(arr_dj[i].style[x]));
var intcontent = arr_dj[i].style[x];
$span.tooltip({
items: "div",
tooltipClass: "style_tooltips",
position: {
my: "left-25 top",
at: "right bottom "
},
content: function(e) {
var html = getmusicdef(intcontent);
return html;
}
});
$divstyle.append($span);
}
}
变量 intcontent
在 content
函数中作为闭包可用,但在函数执行时它被设置为最后一个值。使用 IIFE 将其设置为每次迭代的正确值:
for(var x=0; x<arr_dj[i].style.length; x++){
if(x != 0){//not sure what you wanted to do here, posted code has too many }
$divstyle.append($('<span> / </span>'));
var $span = $('<span></span>');
$span.append(getconststr(arr_dj[i].style[x]));
var intcontent = arr_dj[i].style[x];
$span.tooltip({
items: "div",
tooltipClass: "style_tooltips",
position: {
my: "left-25 top",
at: "right bottom "
},
content: (function(intcontent){
return function(e) {
var html = getmusicdef(intcontent);
return html;
}
}(intcontent))//IIFE setting intcontent correctly
});
$divstyle.append($span);
}
}
变量 intcontent
对于所有工具提示元素始终是相同的值。如何将正确的值传递给工具提示内容函数?
for(var x=0; x<arr_dj[i].style.length; x++){
if(x != 0)$divstyle.append($('<span> / </span>'));
var $span = $('<span></span>');
$span.append(getconststr(arr_dj[i].style[x]));
var intcontent = arr_dj[i].style[x];
$span.tooltip({
items: "div",
tooltipClass: "style_tooltips",
position: {
my: "left-25 top",
at: "right bottom "
},
content: function(e) {
var html = getmusicdef(intcontent);
return html;
}
});
$divstyle.append($span);
}
}
变量 intcontent
在 content
函数中作为闭包可用,但在函数执行时它被设置为最后一个值。使用 IIFE 将其设置为每次迭代的正确值:
for(var x=0; x<arr_dj[i].style.length; x++){
if(x != 0){//not sure what you wanted to do here, posted code has too many }
$divstyle.append($('<span> / </span>'));
var $span = $('<span></span>');
$span.append(getconststr(arr_dj[i].style[x]));
var intcontent = arr_dj[i].style[x];
$span.tooltip({
items: "div",
tooltipClass: "style_tooltips",
position: {
my: "left-25 top",
at: "right bottom "
},
content: (function(intcontent){
return function(e) {
var html = getmusicdef(intcontent);
return html;
}
}(intcontent))//IIFE setting intcontent correctly
});
$divstyle.append($span);
}
}