Change Twitter Bootstrap Tooltip content on click

前端 未结 25 1047
情深已故
情深已故 2020-11-27 10:02

I have a tooltip on an anchor element, that sends an AJAX request on click. This element has a tooltip (from Twitter Bootstrap). I want the tooltip content to change when th

相关标签:
25条回答
  • 2020-11-27 10:32

    I only tested this in bootstrap 4 & w/o calling .show()

    el.tooltip('hide').attr('data-original-title', 'message');
    
    0 讨论(0)
  • 2020-11-27 10:33

    for Bootstrap 4:

    $(element).attr("title", "Copied!").tooltip("_fixTitle").tooltip("show").attr("title", "Copy to clipboard").tooltip("_fixTitle");
    
    0 讨论(0)
  • 2020-11-27 10:33

    This works if the tooltip has been instantiated (possibly with javascript):

    $("#tooltip_id").data('tooltip').options.title="New_value!";
    $("#tooltip_id").tooltip('hide').tooltip('show');
    
    0 讨论(0)
  • 2020-11-27 10:33

    Bootstrap 4

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

    https://getbootstrap.com/docs/4.1/components/tooltips/#tooltipdispose

    $('#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>

    0 讨论(0)
  • 2020-11-27 10:34

    I think Mehmet Duran is almost right, but there were some problems when using multiple classes with the same tooltip and their placement. The following code also avoids js errors checking if there is any class called "tooltip_class". Hope this helps.

    if (jQuery(".tooltip_class")[0]){    
    
            jQuery('.tooltip_class')
                .attr('title', 'New Title.')
                .attr('data-placement', 'right')
                .tooltip('fixTitle')
                .tooltip('hide');
    
        }
    
    0 讨论(0)
  • 2020-11-27 10:39

    For BS4 (and BS3 with minor changes) .. after hours of searching and trials, i came up with the most reliable solution for this problem and it even solves the problem of opening more than one (tooltip or popover) at the same time, and the problem of opening automatically after losing focus, etc.

    $('[data-toggle="tooltip"]').tooltip({
      trigger: 'hover'
    }).on('shown.bs.tooltip', function() {
      var link = $(this);
      var data = '';
      data += '<ul style="list-style-type:none;margin:0;padding:0;text-align:left;font-size:12px">';
      data += '	<li>Sherif Salah</li>';
      data += '	<li>Muhammad Salah</li>';
      data += '	<li>and a gazillion more...</li>';
      data += '</ul>';
      link.attr('data-original-title', data);
      setTimeout(function() {
        if (link.is(':hover')) {
          link.tooltip("dispose").tooltip("show");
        }
      }, 1000);
    });
    <!doctype html>
    <html lang="en">
    
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
    </head>
    
    <body>
      <div class="card">
        <div class="card-body text-center">
          <a href="JavaScript:void(0);" class="btn btn-sm btn-link" data-toggle="tooltip" data-placement="top" data-html="true" title="Loading...">gazillion</a>
        </div>
      </div>
      <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    </body>
    
    </html>

    0 讨论(0)
提交回复
热议问题