Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用getJSON中的数据填充工具提示_Javascript_Jquery_Json - Fatal编程技术网

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,我喜欢让用户等待:)