Bootstrap's tooltip doesn't disappear after button click & mouseleave

前端 未结 22 906
感情败类
感情败类 2020-12-07 16:24

I have a problem with bootstrap\'s tooltip : When I click on a button, tooltip stays even if is the cursor is outside of the button. I have looked into the manual - Bootstra

相关标签:
22条回答
  • 2020-12-07 16:52

    My problem is in DataTable. Below code works for me.

    columnDefs: [ { targets: 0, data: "id", render: function (data, type, full, meta) { return '<a href="javascript:;" class="btn btn-xs btn-default" data-toggle="tooltip" title="Pending" data-container="body"><i class="fa fa-check"></i></a>'; } } ], drawCallback: function() { $('[data-toggle="tooltip"]').tooltip(); $('[data-toggle="tooltip"]').on('click', function () { $(this).tooltip('hide'); }); }

    0 讨论(0)
  • 2020-12-07 16:53

    Using delegation to make it ajax friendly,

    // if you're not using turbolinks, then you can remove this parent wrapper
    $(document).on('turbolinks:load'), function() {  
      // enable tooltip with delegation to allow ajax pages to keep tooltip enabled
      $('body').tooltip({
        select: "[data-toggle='tooltip']"
      });
    
      // remove old tooltip from sticking on the page when calling ajax with turbolinks
      $('body').on('click', "[data-toggle='tooltip']", function() {
        $(this).tooltip('dispose');
      });
    });
    
    0 讨论(0)
  • 2020-12-07 16:56

    I know over a year old, but I couldn't get this to work with any examples here. I've had to use the following:

    $('[rel="tooltip"]').on('click', function () {
        $(this).tooltip('hide')
    })
    

    This also shows the tooltip again upon hover.

    0 讨论(0)
  • 2020-12-07 16:59

    The way I fixed this issue was by removing the tooltip in the click event function using the following code:

    $("#myButton").on("click", function() {
        $("div[role=tooltip]").remove();
    });
    
    0 讨论(0)
  • 2020-12-07 17:00

    Try using rel="tooltip" instead of data-toggle="tooltip" which worked in my case. I was using data-toggle="tooltip" and also set the trigger condition as hover which didn't work in my case. When I changed my data selector, it worked.

    HTML Code:

    <button id="submit-btn" type="submit" class="btn btn-success" rel="tooltip" title="Click to submit this form">Submit</button>
    

    JS Code //Tooltip $('.btn').tooltip({ trigger: 'hover' });

    This will definitely remove the stuck tooltip.

    0 讨论(0)
  • 2020-12-07 17:01

    This is because trigger is not set. The default value for trigger is 'hover focus', thus the tooltip stay visible after a button is clicked, until another button is clicked, because the button is focused.

    So all you have to do is to define trigger as 'hover' only. Below the same example you have linked to without persisting tooltips after a button is clicked :

    $('[data-toggle="tooltip"]').tooltip({
        trigger : 'hover'
    })  
    

    the doc example in a fiddle -> http://jsfiddle.net/vdzvvg6o/

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