【问题标题】:Bootstrap Tooltip - Hide when another tooltip is click引导工具提示 - 单击另一个工具提示时隐藏
【发布时间】:2013-06-20 21:07:51
【问题描述】:

希望有人能帮忙。

我试图在单击另一个工具提示图标时隐藏工具提示。它可以工作,但是当我决定再次单击最后一个工具提示时,它会“闪烁”工具提示。

var Hastooltip = $('.hastooltip');
HasTooltip.on('click', function(e) {
     e.preventDefault();
     HasTooltip.tooltip('hide');
}).tooltip({
     animation: true
}).parent().delegate('.close', 'click', function() {
     HasTooltip.tooltip('hide');
});

HTML

<a href="#" class="hastooltip" data-original-title="Lorem ipsum dolor sit amet, consectetur adipisicing elit.">
    <h3>Info 1</h3>
</a>

<a href="#" class="hastooltip" data-original-title="Lorem ipsum dolor sit amet, consectetur adipisicing elit.">
    <h3>Info 2</h3>
</a>

如果它有助于在用户单击按钮以显示工具提示时将以下标记添加到 DOM。

<div class="tooltip"</div>

【问题讨论】:

  • 无法得到您的问题..工具提示或弹出框?因为工具提示在鼠标离开时消失
  • 使用工具提示。 twitter.github.com/bootstrap/javascript.html#tooltips - 如果工具提示可见并且单击了另一个工具提示,我想要它;可见的工具提示将隐藏。
  • 你也可以发布你的 HTML 吗?
  • 好的,为你添加了。

标签: javascript jquery twitter-bootstrap tooltip


【解决方案1】:

我会给你一个好的解决方案加奖金

        //save the tooltip in variable (change the selector to suit your tooltip)
        var $tooltips = $('a[data-toggle="tooltip"]');
        
        //initialise the tooltip with 'click' trigger  
        $tooltips.tooltip({
            animated: 'fade',
            placement: 'top',
            trigger: 'click',
            delay: { "show": 100, "hide": 100 }
        });
        
        //Here is the juicy bit: when a tooltip is opened it 
        //it creates an 'aria-describedby' with the id of the tooltip
        //opened we can leverage this to turn off all others but current

        $tooltips.on('click', function () {
            var toolTipId = $(this).attr('aria-describedby');
            $('.tooltip').not('#'+ toolTipId).tooltip('hide');
        });

        //But wait theres more! if you call now we chuck in a free close on X seconds event!
        $tooltips.on('shown.bs.tooltip', function (e) {
            //Auto hide after 7 seconds
            setTimeout(function () {
                $(e.target).tooltip('hide');
            }, 7000);
        });

        //call now! XD

【讨论】:

    【解决方案2】:

    回复kiprainey’s answer,有一个问题是,一旦工具提示被隐藏,需要点击两次才能再次显示。我通过使用tooltip('hide') 而不是hide() 解决了这个问题:

    $(element).on('show.bs.tooltip', function() {
        // Only one tooltip should ever be open at a time
        $('.tooltip').not(this).tooltip('hide');
    });
    

    【讨论】:

      【解决方案3】:

      我稍微修改了kiprainey的代码

      const $tooltip = $('[data-toggle="tooltip"]');
       $tooltip.tooltip({
         html: true,
         trigger: 'click',
         placement: 'bottom',
       });
       $tooltip.on('show.bs.tooltip', () => {
         $('.tooltip').not(this).remove();
       });
      

      我使用 remove() 而不是 hide()

      【讨论】:

      • remove() 更好,尤其是当您试图摆脱动态替换元素上的工具提示时。
      • 太棒了。它工作正常。 hide() 实际上隐藏但不会再次显示。但是 remove() 没有这个问题。我在一个元素中同时使用了工具提示和确认,这个 remove() 非常有用
      【解决方案4】:
      $('[data-toggle=tooltip],[rel=tooltip]').tooltip({ 
              container: 'body' }).click(function () {
              $('.tooltip').not(this).hide();
          });
      

      【讨论】:

      • 不要将链接发布为答案,而是添加一些文本来解释此答案如何帮助 OP 解决当前问题。谢谢
      【解决方案5】:

      我也在寻找解决此问题的方法,在我看来 $('.tooltip').not(this).hide(); 将绕过您可能附加到触发器的任何引导程序 showshownhidehidden 事件元素。经过一番思考,我想出了以下代码,可以更透明地处理附加事件。

      注意:仅在 firefox 和 chrome 上测试过,但理论上应该可以正常工作。

      $(document).ready(function() {
      
        $('[data-toggle="popover"]').popover();
      
      
        $(document).on('show.bs.popover', function(event) {
          // could use [data-toggle="popover"] instead
          // using a different selector allows to have different sets of single instance popovers.
          $('[data-popover-type="singleton"]').not(event.target).each(function(key, el) {
            $(el).popover('hide'); // this way everything gets propagated properly
          });
        });
      
        $(document).on('click', function(event) {
          // choose to close all popovers if clicking on anything but a popover element.
          if (!($(event.target).data('toggle') === "popover" /* the trigger buttons */ 
                || $(event.target).hasClass('popover') /* the popup menu */
                || $(event.target).parents('.popover[role="tooltip"]').length /* this one is a bit fiddly but also catches child elements of the popup menu. */ )) {
            
            $('[data-toggle="popover"]').popover('hide');
          }
        });
      
      
      });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
      <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />
      
      
      
      <button type="button" class="btn btn-danger" data-placement="bottom" data-toggle="popover" title="Popover One" data-content="Popover One Content. `focus` trigger still behaves as expected" data-trigger="focus" data-popover-type="singleton">Popover One</button>
      
      <button type="button" class="btn btn-warning" data-placement="bottom" data-toggle="popover" title="Popover Two" data-content="Popover Two Content. for other triggers, clicking on content does not close popover" data-trigger="click" data-popover-type="singleton">Popover Two</button>
      
      <button type="button" class="btn btn-success" data-placement="bottom" data-toggle="popover" title="Popover Three" data-content="Popover Three Content. clicking outside popover menu closes everything" data-trigger="click" data-popover-type="singleton">Popover Three</button>

      这里的小提琴示例:http://jsfiddle.net/ketwaroo/x6k1h7j4/

      【讨论】:

        【解决方案6】:

        这比上述答案更容易处理。您可以在显示处理程序中使用一行 javascript 来执行此操作:

        $('.tooltip').not(this).hide();
        

        这是一个完整的例子。更改“元素”以匹配您的选择器。

        $(element).on('show.bs.tooltip', function() {
            // Only one tooltip should ever be open at a time
            $('.tooltip').not(this).hide();
        });
        

        建议在此 SO 线程中使用相同的技术关闭弹出框:

        How can I close a Twitter Bootstrap popover with a click from anywhere (else) on the page?

        【讨论】:

        • 这是天才,应该是新接受的答案!谢谢!
        • 正是我一直在寻找的。 $('.tooltip').tooltip('隐藏');没用,但是 $('.tooltip').not(this).hide();做到了!
        • 这应该是答案。
        • 不幸的是,当同时使用hoverclick 作为触发器时,这会出现一些问题。
        • 这对我有用,除了一旦工具提示被隐藏,要再次显示它,我必须点击它两次
        【解决方案7】:

        感谢 Jochen 在工具提示上单击“Iphone”以关闭解决方案,这正是我想要的。

        至于原始请求(当您被要求实现单击工具提示而不是翻转工具提示时,显然需要防止多个工具提示功能),这是我的看法:

        , show: function () { 之后添加:

          // HACK BEGIN
          // Quick fix. Only one tooltip should be visible at all time.
          // prototype level property are accessible to all instances so we use one to track last opened tooltip (ie. current this).
          if ( (Tooltip.prototype.currentlyShownTooltip != null) || (Tooltip.prototype.currentlyShownTooltip != undefined) ) {
            // Close previously opened tooltip.
            if (Tooltip.prototype.currentlyShownTooltip != this) { // Conflict with toggle func. Re-show.
                Tooltip.prototype.currentlyShownTooltip.hide();
                Tooltip.prototype.currentlyShownTooltip = null
            }
          }
          // Keep track of the currently opened tooltip.
          Tooltip.prototype.currentlyShownTooltip = this
          // HACK END
        

        【讨论】:

          【解决方案8】:

          您需要检查工具提示是否正在显示并手动切换其可见性。这是一种方法。

          $(function() {
            var HasTooltip = $('.hastooltip');
            HasTooltip.on('click', function(e) {
              e.preventDefault();
              var isShowing = $(this).data('isShowing');
              HasTooltip.removeData('isShowing');
              if (isShowing !== 'true')
              {
                HasTooltip.not(this).tooltip('hide');
                $(this).data('isShowing', "true");
                $(this).tooltip('show');
              }
              else
              {
                $(this).tooltip('hide');
              }
          
            }).tooltip({
              animation: true,
              trigger: 'manual'
            });
          });
          

          【讨论】:

          • 您可以简单地通过删除动画来做到这一点,但这会改变外观。
          • 是否可以切换工具提示?因此,如果我单击已设置为 true 的相同工具提示,它将被设置为隐藏。
          • 一团糟!看起来像是用大锤拍苍蝇的经典例子。
          【解决方案9】:

          对于常规工具提示,我遇到了同样的问题。在 iPhone 上,当点击身体(即其他地方)时,它们不会消失。

          我的解决方案是,当您单击工具提示本身时,它会隐藏。恕我直言,这应该集成在引导程序分发中,因为它的代码很少,效果很大。

          当您可以访问引导源时,添加

          this.tip().click($.proxy(this.hide, this))
          

          作为 tooltip.js 文件中 Tooltip.prototype.init 方法的最后一行:

          Tooltip.prototype.init = function (type, element, options) {
          this.enabled  = true
          this.type     = type
          this.$element = $(element)
          this.options  = this.getOptions(options)
          
          var triggers = this.options.trigger.split(' ')
          
          for (var i = triggers.length; i--;) {
            var trigger = triggers[i]
          
            if (trigger == 'click') {
              this.$element.on('click.' + this.type, this.options.selector, $.proxy(this.toggle, this))
            } else if (trigger != 'manual') {
              var eventIn  = trigger == 'hover' ? 'mouseenter' : 'focus'
              var eventOut = trigger == 'hover' ? 'mouseleave' : 'blur'
          
              this.$element.on(eventIn  + '.' + this.type, this.options.selector, $.proxy(this.enter, this))
              this.$element.on(eventOut + '.' + this.type, this.options.selector, $.proxy(this.leave, this))
            }
          }
          
          this.options.selector ?
            (this._options = $.extend({}, this.options, { trigger: 'manual', selector: '' })) :
            this.fixTitle()
          
           // Hide tooltip when clicking on it. Useful for mobile devices like iPhone where eventOut
           // (see above) on $element is not triggered and you don't get rid of the tooltip anymore.
           this.tip().click($.proxy(this.hide, this))
            }
          

          如果你手头没有资源,你可以通过以下方式达到同样的效果:

              $(function()
              {
                  // Apply tooltips
                  var hasTooltip = $("[data-toggle='tooltip']").tooltip();
          
                  // Loop over all elements having a tooltip now.
                  hasTooltip.each(function()
                     {
                         // Get the tooltip itself, i.e. the Javascript object
                         var $tooltip = $(this).data('bs.tooltip');
          
                         // Hide tooltip when clicking on it
                         $tooltip.tip().click($.proxy($tooltip.hide, $tooltip))
                     }
                  );
              });
          

          对我来说,这可以在 iPhone 上提供良好的用户体验:单击元素以查看工具提示。单击它消失的工具提示。

          【讨论】:

            猜你喜欢
            • 2016-05-06
            • 1970-01-01
            • 1970-01-01
            • 2020-07-28
            • 1970-01-01
            • 1970-01-01
            • 2021-02-06
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多