Javascript 使用getJSON中的数据填充工具提示
如果我是正确的Javascript 使用getJSON中的数据填充工具提示,javascript,jquery,json,Javascript,Jquery,Json,如果我是正确的getJSON无法返回单词,因为它是一个异步方法。 如何改进下面的代码,用名为words的变量填充标题,该变量包含我要在工具提示title属性中推送的数据 $('.multiselect-container li').not('.filter, .group').tooltip({ placement: 'right', container: 'body', title: function () { var req = $.getJSON('
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
}
});
});
将工具提示创建放在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
}
});
});
将工具提示创建放在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
}
});
});
将工具提示创建放在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
}
});
});
我认为解决方案之一是同步调用ajax函数。这里有一个例子
function getData()
{
if (window.XMLHttpRequest)
AJAX=new XMLHttpRequest();
else
AJAX=new ActiveXObject("Microsoft.XMLHTTP");
if (AJAX)
{
AJAX.open("GET", "localfile.json", false);
AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
AJAX.send();
return JSON.parse(AJAX.responseText);
}
else
return null;
}
那么,您需要在代码中执行以下操作:
$('.multiselect-container li').not('.filter, .group').tooltip({
placement: 'right',
container: 'body',
title: getData
});
我认为解决方案之一是同步调用ajax函数。这里有一个例子
function getData()
{
if (window.XMLHttpRequest)
AJAX=new XMLHttpRequest();
else
AJAX=new ActiveXObject("Microsoft.XMLHTTP");
if (AJAX)
{
AJAX.open("GET", "localfile.json", false);
AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
AJAX.send();
return JSON.parse(AJAX.responseText);
}
else
return null;
}
那么,您需要在代码中执行以下操作:
$('.multiselect-container li').not('.filter, .group').tooltip({
placement: 'right',
container: 'body',
title: getData
});
我认为解决方案之一是同步调用ajax函数。这里有一个例子
function getData()
{
if (window.XMLHttpRequest)
AJAX=new XMLHttpRequest();
else
AJAX=new ActiveXObject("Microsoft.XMLHTTP");
if (AJAX)
{
AJAX.open("GET", "localfile.json", false);
AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
AJAX.send();
return JSON.parse(AJAX.responseText);
}
else
return null;
}
那么,您需要在代码中执行以下操作:
$('.multiselect-container li').not('.filter, .group').tooltip({
placement: 'right',
container: 'body',
title: getData
});
我认为解决方案之一是同步调用ajax函数。这里有一个例子
function getData()
{
if (window.XMLHttpRequest)
AJAX=new XMLHttpRequest();
else
AJAX=new ActiveXObject("Microsoft.XMLHTTP");
if (AJAX)
{
AJAX.open("GET", "localfile.json", false);
AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
AJAX.send();
return JSON.parse(AJAX.responseText);
}
else
return null;
}
那么,您需要在代码中执行以下操作:
$('.multiselect-container li').not('.filter, .group').tooltip({
placement: 'right',
container: 'body',
title: getData
});
如果要让用户在请求工具提示时等待,请确保…;)同步XMLHttpRequest请求应该尽可能避免,因为它们在等待响应时会阻塞UI(在这里可以避免,所以我建议@PitaJ的答案)。此外,如果要同步执行,可以使用jQuery的
async:false
选项。@CaseyFalk,我喜欢让用户等待:)如果要让用户在请求工具提示时等待,请确保…;)同步XMLHttpRequest请求应该尽可能避免,因为它们在等待响应时会阻塞UI(在这里可以避免,所以我建议@PitaJ的答案)。此外,如果要同步执行,可以使用jQuery的async:false
选项。@CaseyFalk,我喜欢让用户等待:)如果要让用户在请求工具提示时等待,请确保…;)同步XMLHttpRequest请求应该尽可能避免,因为它们在等待响应时会阻塞UI(在这里可以避免,所以我建议@PitaJ的答案)。此外,如果要同步执行,可以使用jQuery的async:false
选项。@CaseyFalk,我喜欢让用户等待:)如果要让用户在请求工具提示时等待,请确保…;)同步XMLHttpRequest请求应该尽可能避免,因为它们在等待响应时会阻塞UI(在这里可以避免,所以我建议@PitaJ的答案)。此外,如果要同步执行,可以使用jQuery的async:false
选项。@CaseyFalk,我喜欢让用户等待:)