3

我正在尝试根据锚标记的 id 属性调用不同的工具提示。我的 JavaScript 代码如下:

$(function()
{
   $('.tippy').qtip(
   {
      content: {
          url: "http://mydomain.com/product/mini/" + $(this).attr("id") // doesn't work
      },

      hide: { when: 'mouseout', fixed: true },

       position: {
         corner: {
            target: 'bottomRight',
            tooltip: 'topLeft'
         }
      }
   });

});

我的 html 代码如下所示:

<div>this is the text and I would like to reference the <a href="product.php" class="tippy" id="123456">superproduct</a> with qtip.</div>

我卡住了,你能帮我一把吗?

4

1 回答 1

4

使用each()

$('.tippy').each(function() {  
   $(this).qtip({
          content: {
              url: "http://mydomain.com/product/mini/" + $(this).attr("id")
          },
          hide: { when: 'mouseout', fixed: true },   
          position: {
             corner: {
                target: 'bottomRight',
                tooltip: 'topLeft'
             }
          }
   });
)};

$(this)在您的代码中不起作用的原因很简单:它不在函数调用内部(至少不this应该在哪里引用.tippy元素)。$(this).attr()在构造传递给 的对象时执行qtip()。这意味着它在相同的上下文中$('.tippy'),因此this很可能是指window.

于 2010-07-16T11:46:36.827 回答