如何在jquery qTips中获取selector属性?

时间:2011-07-06 17:59:28

标签: javascript jquery

我正在尝试获取也调用jQuery qTips的标记的“id”属性。设置如下:

$('.selector').qtip({
   content: {
      text: 'Loading...', // The text to use whilst the AJAX request is loading
      ajax: {
         url: 'getInfo.php', // URL to the local file
         type: 'GET', // POST or GET
         async: false,
         data: { id: $(this).attr('id')}, // !! PROBLEM HERE
         success: function(data, status) {

            // Set the content manually (required!)
            this.set('content.text', data);
         }
      }
   }

});

来电者是这样的:

<a href='http://www.google.com' class='selector' id='Jerome'>Jerome</a>

但由于某种原因,$(this).attr(“id”)未定义。 qTip显示但是空白,并且GET调用显示为未在Firebug中传递任何数据。我错过了什么?

谢谢!

编辑:同样,this.attr返回“this.attr不是函数”

3 个答案:

答案 0 :(得分:3)

试试这个可以解决你的问题。

$('.selector').each(function(index, item){
  $(item).qtip({
   content: {
      text: 'Loading...', // The text to use whilst the AJAX request is loading
      ajax: {
         url: 'getInfo.php', // URL to the local file
         type: 'GET', // POST or GET
         async: false,
         data: { id: $(item).attr('id')}, // !! PROBLEM HERE
         success: function(data, status) {

            // Set the content manually (required!)
            this.set('content.text', data);
         }
      }
   }

});

});

答案 1 :(得分:0)

这不是指向当前元素,因为它仍在构造要传递给qtip插件的json对象。你必须使用相同的选择器来获取jquery对象。

$('.selector').qtip({
   content: {
      text: 'Loading...', // The text to use whilst the AJAX request is loading
      ajax: {
         url: 'getInfo.php', // URL to the local file
         type: 'GET', // POST or GET
         async: false,
         data: { id: $('.selector').attr('id')}, // !! PROBLEM HERE
         success: function(data, status) {

            // Set the content manually (required!)
            this.set('content.text', data);
         }
      }
   }

});

答案 2 :(得分:0)

这个怎么样:

$('.selector').each(function () {
        var thisId = $(this).attr("id");
        $(this).qtip({
            content: {
                text: 'Loading...', // The text to use whilst the AJAX request is loading
                ajax: {
                    url: 'getInfo.php', // URL to the local file
                    type: 'GET', // POST or GET
                    async: false,
                    data: { id: thisId }, // !! PROBLEM HERE
                    success: function (data, status) {

                        // Set the content manually (required!)
                        this.set('content.text', data);
                    }
                }
            }
        });
    });