How to overwrite twitter bootstrap tooltip?

2019-02-12 07:41发布

I'm using tooltips from Twitter Bootstrap package to show items information on the page. Sometimes information is changed, and needs to be updated in the tooltip. I tried simply to reinitialize tooltip with new title:

$('#selector').tooltip({ title: 'new text'});

However tooltip's title doesn't get changed with the new text. It remains the same as it was set initially. Any ideas why, and is there any work around? Thanks!

6条回答
We Are One
2楼-- · 2019-02-12 08:13

this works in bootstrap 3, above all other solutions given in this thread:

$('#tooltip_selector').attr('data-original-title', 'New Title Here');
查看更多
我想做一个坏孩纸
3楼-- · 2019-02-12 08:15

I use bootstrap 3, and none of these worked for me without bugs. Destroying the tooltip via .tooltip('destroy') and recreating it in the same function .tooltip({...}) would result in the tooltip no longer working. It only worked if the recreation call was done later, after the destroy call had completed it's work asynchronously (hence why the immediate recreation was clobbered).

The suggestions to destroy the internal data .data('tooltip',false) did not work. Firstly, with bootstrap 3, it would be data('bs.tooltip, false) and when that was called, if the tooltip was currently displayed, it would leak it and leave it displayed permanently. The new tooltip would both display and hide over top of it.

My final solution was:

$x.tooltip('hide');
$x.data('bs.tooltip', false);
$x.tooltip({ ... }) // recreate it

Note that also changing the title via the .attr('title', '...') and similar solutions and calling .tooltip('fixupTitle') worked - but discarded the placement option and likely also the other options, and made a tooltip that was placed at the top the element rather than on the right, as it originally was. It might even have had some of the other bugs listed above, seen in other variations of this solution, but I didn't look much further.

查看更多
你好瞎i
4楼-- · 2019-02-12 08:28

@Sherbrow You can use the simplest one.Need not to go this complicated solution. Here is the sample code.

$('#spanUsername').prop('title', newValue);     
查看更多
Fickle 薄情
5楼-- · 2019-02-12 08:29

Update : As pointed out by @Nigel-Angel, TWBS v3 uses data('bs.tooltip')


You can't override the tooltip object once you have initialized it on one element. But you can delete it and do it again.

Try deleting and then reinitialize the tooltip with all your options (if you had any).

$('#selector').data('tooltip',false)          // Delete the tooltip
              .tooltip({ title: 'new text'});

There may be a need to remove the listeners, but it works like that.

查看更多
Viruses.
6楼-- · 2019-02-12 08:30

For some reason, this did not work for me (bootstrap 2.1.1). What I had to do was:

    $('#element').tooltip('destroy');
    $("#element").tooltip({'placement':'right', 'title':'larger larger text title'});
    $("#element").tooltip('show');
查看更多
老娘就宠你
7楼-- · 2019-02-12 08:35

This technique did not work for me so I found an answer, hidden in one of the comments of a similar question.

the cleanest way to update the display text of the tooltip

$(element).attr('title', 'NEW_TITLE').tooltip('fixTitle').tooltip('show');

Thank you to lukmdo

查看更多
登录 后发表回答