如果我是正确的,getJSON
无法返回这些单词,因为它是异步方法。
如何改进以下代码,以使用名为 words
的变量填充标题,该变量包含我想要在工具提示 title
属性中推送的数据?
$('.multiselect-container li').not('.filter, .group').tooltip({
placement: 'right',
container: 'body',
title: function () {
var req = $.getJSON('localfile.json', function(data) {
var words = data["blarg"]["something"];
});
return "content"; // words variable should fit here
}
});
最佳答案
将工具提示创建放在ajax回调函数中
$.getJSON('localfile.json', function(data) {
var words = data["blarg"]["something"];
$('.multiselect-container li').not('.filter, .group').tooltip({
placement: 'right',
container: 'body',
title: function () {
return words; // words variable should fit here
}
});
});
关于javascript - 使用 getJSON 中的数据填充工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25294459/