Bootstrap:将鼠标悬停在工具提示文本上以单击 link

Bootstrap: hover over the tooltip text to click a link

我在我的网络应用程序中使用 Bootstrap 3.x 中的工具提示工具来显示有用的信息。它以这种方式工作:

$('[data-toggle="tooltip"]').tooltip({
        'animation': true,
        'placement': 'top',
        'trigger': 'hover', 
        'html':true,
        'container': 'body'
});

请注意,我必须使用悬停来触发工具提示。现在我需要添加一个新的工具提示,它的工具提示文本包含一个 HTML link。访问者应该能够将鼠标悬停在工具提示文本上以单击 link.

问题是当访问者移动他的鼠标时,工具提示文本在他到达工具提示文本区域之前就消失了。

时间差或许可以解决您的问题。

var originalLeave = $.fn.popover.Constructor.prototype.leave;
$.fn.popover.Constructor.prototype.leave = function(obj){
  var self = obj instanceof this.constructor ?
    obj : $(obj.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
  var container, timeout;

  originalLeave.call(this, obj);

  if(obj.currentTarget) {
    container = $(obj.currentTarget).siblings('.popover')
    timeout = self.timeout;
    container.one('mouseenter', function(){
      //We entered the actual popover – call off the dogs
      clearTimeout(timeout);
      //Let's monitor popover content instead
      container.one('mouseleave', function(){
        $.fn.popover.Constructor.prototype.leave.call(self, self);
      });
    })
  }
};


$('body').popover({ selector: '[data-popover]', trigger: 'click hover', placement: 'auto', delay: {show: 50, hide: 400}});

  
  
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<p id='container'>
<button class='btn btn-primary btn-large' data-popover="true" data-html=true data-content="<a href='http://www.wojt.eu' target='blank' >click me, I'll try not to disappear</a>">hover here</button>
</p>

现在正在使用工具提示。看看这是否能更好地回答您的问题。

var originalLeave = $.fn.tooltip.Constructor.prototype.leave;
$.fn.tooltip.Constructor.prototype.leave = function(obj) {
  var self = obj instanceof this.constructor ?
    obj : $(obj.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
  var container, timeout;

  originalLeave.call(this, obj);

  if (obj.currentTarget) {
    container = $(obj.currentTarget).siblings('.tooltip')
    timeout = self.timeout;
    container.one('mouseenter', function() {
      //We entered the actual popover – call off the dogs
      clearTimeout(timeout);
      //Let's monitor popover content instead
      container.one('mouseleave', function() {
        $.fn.tooltip.Constructor.prototype.leave.call(self, self);
      });
    })
  }
};


$('body').tooltip({
  selector: '[data-toggle]',
  trigger: 'click hover',
  placement: 'auto',
  delay: {
    show: 50,
    hide: 400
  }
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<p>
  <button class='btn btn-primary btn-large' data-toggle="tooltip" data-html="true" title="<a href='http://www.wojt.eu' target='blank' >click me, I'll try not to disappear</a>">hover here</button>
</p>

这对我适用于 bootstrap 4.1.3 使用记录的事件(没有补丁!)

$(document).ready(function() {
    $('body').on('inserted.bs.tooltip', function(e) {
        var $target = $(e.target);

        // Keep track so we can check if mouse is hovering over the tooltip
        $('[role="tooltip"]').hover( function() {
            $(this).toggleClass('hover');
        });

        $target.on('hide.bs.tooltip', function(e) {
            // If tooltip is under the mouse, prevent hide but
            // add handler to hide when mouse leaves tooltip
            if ($('[role="tooltip"]').hasClass('hover')) {
                $('[role="tooltip"]').on('mouseleave', function() {
                    setTimeout(function() {
                            $target.tooltip('hide');
                        }, yourHideDelay);
                });
                // Tell bootstrap tooltip to bail and not actually hide
                e.preventDefault();
                return;
            }
        });
    });
});

然后可以 select 工具提示中的文本或单击其中的链接等。