javascript - 使用 javascript 显示和隐藏 Bootstrap 工具提示

标签 javascript jquery html twitter-bootstrap

当用户点击元素且条件为假时,我需要显示 Bootstrap 工具提示。我为此编写了代码:

<div data-toggle="tooltip" title="You must to log in!" class="stars">425</div>

和Javascript:

$(".statistics .stars").click( function(){
    if (! user.isLogin){
        $(this).tooltip("show");

        setTimeout(function(){
          $(this).tooltip( 'hide' );
        }, 2000);   
    }
});

当我不点击时,我可以在悬停时看到默认的工具提示(我不需要它),当我点击时,工具提示不会在 2 秒后隐藏。如何解决这些问题?

最佳答案

首先你需要将工具提示设置为手动,现在它不会在悬停时弹出

$('div').tooltip({trigger: 'manual'});

之后你需要在 setTimeout 中使用它之前保存 div 元素,因为 setTimeout 之外的 this 和 setTimeout 内部的 this 是不同的。

$('div').click(function(){
  var tt = $(this);
  if (! user.isLogin){
    tt.tooltip("show");

    setTimeout(function(){
      tt.tooltip( 'hide' );
    }, 2000);   
  }
});

这是更新的 jsfiddle

关于javascript - 使用 javascript 显示和隐藏 Bootstrap 工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37938425/

相关文章:

Javascript:将方法添加到数组中的所有对象

javascript - 引用 JavaScript 父对象时如何避免 "variable may not have been initialized"?

javascript - WordPress插件tinymce冲突

jquery - 当我悬停另一个元素时,如何将悬停样式应用于 div?

javascript - 使用 jQuery 预加载目录中的所有图像

javascript - Eclipse:内容辅助不显示建议

javascript - 使用事件时在关闭 body 标签之前使用 script 标签的优点

javascript - jquery/javascript : add to array in case tr has certain class

javascript - Google +1 按钮和推文按钮的问题

javascript - Bootstrap NavBar 折叠在默认的 angular-fullstack 生成代码中不起作用