jQuery Tools: Tooltip, jQuery reference to “this” attribute

依然范特西╮ 提交于 2019-12-12 01:13:48

问题


I'm implementing a Tooltip for cells within table and I need to be able to grab the target's data-message attribute.

$("#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
    });

That doesn't work. I get "Cannot find tooltip for [object Object]" ... I'm not quite sure how to reference the target correctly.


回答1:


I don't think you can use this inside a map like that. Try:

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



回答2:


I'm not sure how you do this inside the tooltip plugin however you can use an .each() loop to get the value and initialize the tooltip plugin for each element (internally this is all the plugin probably does anyway):

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

    });
});


来源:https://stackoverflow.com/questions/8301536/jquery-tools-tooltip-jquery-reference-to-this-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!