2011-11-28 27 views
0

我正在实施表格中单元格的工具提示,我需要能够抓取目标的data-message属性。jQuery工具:工具提示,jQuery引用“this”属性

$("#pricing_plans_table .sku_tooltip").tooltip({ 

     // each trashcan image works as a trigger 
     tip: '#' + $(this).attr('data-message'), 

     // move tooltip a little bit to the right 
     offset: [0, 15], 

     // there is no delay when the mouse is moved away from the trigger 
     delay: 0 
    }); 

这是行不通的。我得到“找不到[对象对象]的工具提示”...我不太清楚如何正确引用目标。

+0

这一切都看起来是正确的,我们可以看到html以及? –

+0

console.log'$(this).attr('data-message')' – CamelCamelCamel

回答

0

我不认为你可以在这样的地图内使用this。尝试:

$("#pricing_plans_table .sku_tooltip").each(function(i,el) { 
    var tipstr = $(this).attr('data-message'); 
    $(this).tooltip({ 
     tip: '#' + tipstr, 
     offset: [0, 15], 
     delay: 0 
    });  
}); 
+0

只要您知道“this”指的是什么,就可以在地图内部使用'this'。在这种情况下,OP认为'this'会引用他称之为'tooltip'的对象,而不是引用this在外部上下文中的任何内容。只是澄清一下:'这个'是完全有效的,你只需要知道'this'是什么! –

+0

@查德并不完全,请参阅AdamTerlson的回答。 – Blazemonger

+0

我的印象是有一个他正在分配的单个项目;这是我的误解。我删除了我的评论。 – Chad

1

我不知道你是怎么做到这一点tooltip插件里面,但是你可以使用.each()循环来获取值并初始化tooltip插件的每个元素(在内部,这是所有的插件可能不无论如何):

$("#pricing_plans_table .sku_tooltip").each(function (index, value) { 
    var $this = $(this); 
    $this.tooltip({ 
     tip : '#' + $this.attr('data-message'), 
     offset : [0, 15], 
     delay : 0 

    }); 
}); 
+0

@Chad使用'.each()'循环的原因是为正确的'tooltip'实例分配正确的'data-message'属性。你如何做到这一点呢? – Jasper

+0

我的印象是有一个他正在分配的单个项目;这是我的误解。我删除了我的评论。 – Chad

+0

乍得做了和我一样的假设:这是一个单一的元素。相反,OP似乎试图创建多个工具提示。 –