Javascript jQuery变量选择器不使用find()

Javascript jQuery变量选择器不使用find(),javascript,jquery,css,Javascript,Jquery,Css,我试图将唯一ID作为变量传递,但遇到了问题 <span id="e-tt1_1" class="tt1">Trigger 3</span> <span id="e-tt1_2" class="tt1">Trigger 3</span> <span id="e-tt1_3" class="tt1">Trigger 3</span> 我试图将变量中的唯一ID传递给新创建的DIV中的parsedHTML Ajax响应,尽管控制台

我试图将唯一ID作为变量传递,但遇到了问题

<span id="e-tt1_1" class="tt1">Trigger 3</span>
<span id="e-tt1_2" class="tt1">Trigger 3</span>
<span id="e-tt1_3" class="tt1">Trigger 3</span>
我试图将变量中的唯一ID传递给新创建的DIV中的parsedHTML Ajax响应,尽管控制台日志指示选择器到达,但它并没有完成该函数。我知道我在做一些基本的错误,可能是因为当ID直接放在代码中时,请参见var元素上方的空白代码,它显示了相关的DIV

$(document).ready(function() {
$('.tt1[id^="e-tt1_"]').each(function() {           
    var $scoop = $('#data_' + $(this).attr('id'));
     $(this).qtip({ 
        content: { 
            text: function(event, api) {                        
                    api.elements.content.html('Loading...');

                    return $.ajax({                                 
                        url: 'pages/tooltip-pricing-essential1.html',
                        dataType: 'html'                             
                        })
                        .then(function(content) {                                   
                            // Append the parsed HTML of the retrieved page to a dummy <div/>, and find all <$scoop> elements
                            //var elements = $("<div />").append( $.parseHTML(content) ).find('#data_e-tt1_2');

                            var elements = $("<div />").append( $.parseHTML(content) ).find($scoop);

                            console.log($scoop);                                    

                            // Set the content of the tooltip to the selected element(s)
                            api.set('content.text', elements);

                        }, function(xhr, status, error) {
                                api.set('content.text', status + ':  ' + error);
                    });
                }
        },
        position: {
            target: 'event', // Use the triggering element as the positioning target
            my: 'bottom center',
            at: 'right center',
            adjust: { x: -15 },
            viewport: $(window)
        },
        style: { classes: 'qtip-light qtip-rounded qtip-shadow' },
        hide: {
         fixed: true,
         delay: 1500
        }
     });
 });
 });
我做错了什么

注意:在QTIPAPI中,该方法需要向其传递文本值

这是一种非常奇怪的方式,我建议不要从服务器获取工具提示文本,或者至少在需要显示它们之前获取它们


另外,请不要在AJAX响应中使用HTML块,而是使用JSON,因为JSON更易于使用。

为什么是$'.tt1[id^=e-tt1\]'而不是$'.tt1'?我不担心,是的,这有点无关紧要,但问题仍然是一样的?你为什么在那时写而不是完成?对于jqueryajax?非常奇怪。不知道为什么,但在整个页面显示结果,然后似乎允许个人div被选中,我可能是错的,但它的工作。只是传递的变量$scoop不会选择DIV,但是如果我将相同的id放入find中,它会选择DIV。代码可以工作,但变量由于某种原因不能工作?请不要使用then,而是使用done,除非您确切知道它的用途不是为了这个,而是为了调用新的承诺/延迟。我也不明白这意味着什么,它没有完成功能。。请具体说明哪种方式不起作用,您期望的是什么,是什么线路导致了问题。对此我感激不尽,它工作得非常完美,我认为您是一位绅士,显然是一位学者,我也学到了一些东西。感谢所有的帮助,尤其是建议,Nooneo@Nooneo-很高兴能提供帮助,更高兴的是你在这个过程中学到了一些东西!将这个问题标记为正确答案会很好:这有点新,不确定我是否已经完成了标记为正确所需的所有工作?
$('.tt1').each(function() {
  var $scoop = '#data_' + this.id; // <-- this MUST be a "string"
  
  $(this).qtip({
    content: {
      text: function(event, api) {
        api.elements.content.html('Loading...');

        fetchTtip("pages/tooltip-pricing-essential1.html") // <-- see below
          .done(function(content) { // <-- PLEASE use "done" method
            // Append the parsed HTML of the retrieved page to a dummy <div/>, and find all <$scoop> elements
            var element = $("<div>").html(content).find($scoop);
   
            // Set the content of the tooltip to the selected element(s)
            api.set('content.text', element.html()); // <-- value MUST be a "string"
            //api.elements.content.html(content);
          })
          .fail(function(xhr, status, error) { // <-- PLEASE use "fail" method
            api.set('content.text', status + ':  ' + error);
          });
      }
    },
    position: {
      target  : 'event', // Use the triggering element as the positioning target
      my      : 'bottom center',
      at      : 'right center',
      viewport: $(window),
      adjust : {
        x: -15
      }
    },
    style: {
      classes: 'qtip-light qtip-rounded qtip-shadow'
    },
    hide: {
      fixed: true,
      delay: 1500
    }
  });
});

function fetchTtip(URL){
  //// TEMPORARY FOR THIS DEMO - (simulate AJAX) ///////
  var deferred = new $.Deferred(),
      mockData = `
      <div id="data_e-tt1_1"><strong>Title 1</strong><br>text 1 - Lorem ipsum dolor sit amet consectetuer adipiscing elit</div>
      <div id="data_e-tt1_2"><strong>Title 2</strong><br>text 2 - aaaa</div>
      <div id="data_e-tt1_3"><strong>Title 3</strong><br>text 3 - bbb bbb</div>`;

  setTimeout(()=>{
    deferred.resolve(mockData);
  }, 1000)

  return deferred;
  //////////////////////////////////////////////////


  return $.ajax({
    url      : URL,
    dataType : 'html'
  })
}