Can't change bootstrap Tooltip title

2020-03-19 03:08发布

I've already looked over several posts on stack overflow asking virtually the exact same question yet none of what I found on those questions has helped. I'm very new to JQuery and Bootstrap so maybe I'm just missing some really simple thing.

I want to be able to to change the title of the tooltip on different elements after the first initialization(ideally multiple times after initialization.) A simplified version of what I'm dealing with:

<canvas id="bag0"  data-toggle="tooltip" title="test">
</canvas>
...
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

$(document).ready(function(){
            $('#bag0').data('tooltip',false)          
                  .tooltip({ title: 'new text'});
            $('[data-toggle="tooltip"]').tooltip();

});

This method to change the title was given from posting: How to overwrite twitter bootstrap tooltip?

The tooltip always reads "test." I've tinkered with a few others things to no avail. I suspect I'm overlooking something obvious.

7条回答
混吃等死
2楼-- · 2020-03-19 03:25

Try following,

$(document).ready(function(){
            $('#bag0').attr('title', 'new text')
                  .tooltip('destroy') // if you are using BS4 use .tooltip('dispose')
                  .tooltip({ title: 'new text'});
            $('[data-toggle="tooltip"]').tooltip('show');

});
<canvas id="bag0"  data-toggle="tooltip" title="test">
</canvas>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

查看更多
走好不送
3楼-- · 2020-03-19 03:29

This might help

$('[data-toggle="tooltip"]').attr("title","NEW TEXT");

If you want to for a particular element, say a <div>

$('div[data-toggle="tooltip"]').attr("title","NEW TEXT");
查看更多
手持菜刀,她持情操
4楼-- · 2020-03-19 03:37

Bootstrap 4

$('#topic_1').tooltip('dispose').tooltip({title: 'Goodbye'}).tooltip('show')

$('#topic_1').tooltip({title: 'Hello'}).tooltip('show');
setTimeout( function() {
  $('#topic_1').tooltip('dispose').tooltip({title: 'Goodbye'}).tooltip('show');
}, 5000);
#topic_1 {
  border: 1px solid red;
  margin: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>

<div id="topic_1">Topic 1</div>

查看更多
Fickle 薄情
5楼-- · 2020-03-19 03:40

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

Above code might help you.

$.tooltip(string) calls any function within the Tooltip class. And if you look at Tooltip.fixTitle, it fetches the data-original-title attribute and replaces the title value with it.

You can use the element id or class to make it more specific.

  • Help :)
查看更多
Fickle 薄情
6楼-- · 2020-03-19 03:48

Change Bootstrap 4 Tooltip title

$(document).ready(function() {
  // initilizing Tooltip
  $('[data-toggle="tooltip"]').tooltip();

  // Get the Tooltip
  let btn_tooltip = $('#my-btn');

  // Change Tooltip Text on mouse enter
  btn_tooltip.mouseenter(function () {
    btn_tooltip.attr('title', 'Default Tooltip').tooltip('dispose');
    btn_tooltip.tooltip('show');
  });

  // Update Tooltip Text on click
  btn_tooltip.click(function () {
    btn_tooltip.attr('title', 'Modified Tooltip').tooltip('dispose');
    btn_tooltip.tooltip('show');
  });
});
<!DOCTYPE html>
<html lang="en-US">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container-fluid">
  <p>Click the button:</p>
  <button id="my-btn" type="button" class="btn btn-primary" data-toggle="tooltip" title="Default tooltip">Click Me</button>
</div>

</body>
</html>

查看更多
成全新的幸福
7楼-- · 2020-03-19 03:50

Try this

$(element).tooltip().attr('data-original-title', "new title");

Source: github bootstrap issue

查看更多
登录 后发表回答