~Onlooker 2016-01-13 15:55 采纳率: 0%
浏览 32

jQuery Hovercard AJAX加载

I have been attempting to integrate jQuery hovercard (http://designwithpc.com/Plugins/Hovercard) with our web application. We need it to display HTML received from an AJAX page when hovering over a username link identified with the data attribute data-toggle="user".

This is our code so far...

$('a[data-toggle="user"]').hovercard({
  detailsHTML: 'Loading user details...',
  width: 350,
  cardImgSrc: 'http://ejohn.org/files/short.sm.jpg', //Dummy URL for now
  onHoverIn: function() {
    // set your twitter id
    var user = 'jeresig';

    $.ajax({
      url: '/components/users/_hovercards/user-hovercard.php',
      type: 'GET',
      dataType: 'text',
      beforeSend: function() {
        $(this).text("Loading...");
      },
      success: function(data) {
        $(this).empty();
        $(this).text(data);
      },
      complete: function() {

      },
      error: function() {
        //$(this).text("An error occured in loading the user hovercard");
        $(this).text("An error occured loading the user data...");
      }
    });
  }
});

The issue is that this does not append the HTML from the AJAX page to the hovercard. I have tried a few changes to diagnose the fault and when I explicitly call the $(this) value and attempt to manually change it outside the AJAX lines to test appending the data manually I wind up replacing the link data itself with the manually appended html and the hovercard does not display.

I have used the global selector in a few spots in my code to apply events to all elements with a particular data-toggle value and use the $(this) selector with no issue but in this instance I am having issues.

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • weixin_33716154 2016-01-13 16:15
    关注

    This is because you $(this) is not the same as $('a[data-toggle="user"]')within the $.ajax call. Instead, the $(this) refers to the AJAX object, which will not do anything.

    Here's a modified solution from this answer:

    $('a[data-toggle="user"]').hovercard({
      ...
      var elem = $(this);
      $.ajax( ...
        success: function(data) {
          elem.empty();
          elem.text(data);
        } ...
        error: function() {
          elem.text("An error occured loading the user data...");
        }
      });
     }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?