简体   繁体   English

禁用按钮解决方法上的Bootstrap工具提示

[英]Bootstrap tooltip on disabled button workaround

I am trying to use a Bootstrap tooltip on a disabled button to let the user know why the button is in such state. 我试图在禁用按钮上使用Bootstrap工具提示让用户知道按钮处于这种状态的原因。 However, the fact that most browsers don't fire a click or hover event on a disabled element makes things harder. 但是,大多数浏览器不会在禁用的元素上触发单击或悬停事件这一事实会使事情变得更加困难。

I am trying to wrap the button in a div and initialize the tooltip in this element, as suggested by this solution I found around SO. 我试图将按钮包装在div中并初始化此元素中的工具提示,正如我在SO周围找到的这个解决方案所建议的那样。

So I am trying this: 所以我想这个:

<div id="disabled-button-wrapper" data-title="Tooltip title">
  <button class="btn btn-primary" disabled="disabled">Button</button>
</div>

<script>
    $(function() {
        $('#disabled-button-wrapper').tooltip();
    });
</script>

jsfiddle 的jsfiddle

But the tooltip is not working at all. 但工具提示根本不起作用。 Actually, it's not even because of the button, no matter what the content is, the tooltip is not working on the wrapper div. 实际上,它甚至不是因为按钮,无论内容是什么,工具提示都不在包装div上工作。 What's going on here? 这里发生了什么? What obvious detail am I missing? 我错过了哪些明显的细节?

Edit: when inspecting with the Chrome dev tools I do actually see a tooltip element is being generated, but there are a couple of issues: 编辑:在使用Chrome开发工具进行检查时,我确实看到正在生成工具提示元素,但存在以下几个问题:

  • It is only triggered when hovering to the right of the button: the div takes all the width (which, by the way, I would like for the parent to be just as wide as the button). 它仅在悬停到按钮右侧时触发:div占用所有宽度(顺便说一下,我希望父级的宽度与按钮一样宽)。 But not when hovering over the button. 但不是在悬停在按钮上时。
  • The tooltip element is hidden somewhere, not being rendered properly. 工具提示元素隐藏在某处,而不是正确呈现。

Edit 2: I made some changes: jsfiddle 编辑2:我做了一些改动: jsfiddle

That way the wrapper doesn't take all the width, and using the body as the container would solve the rendering issue. 这样,包装器不会占用所有宽度,并且使用body作为容器将解决渲染问题。 Now the only remaining issue is: why isn't the hover event being fired on the wrapper when hovering over the part where the disabled button is? 现在唯一剩下的问题是: 当将鼠标悬停在禁用按钮所在的部分上时,为什么不在包装器上触发悬停事件?

If you really really inspect the jsfiddle you can see some css in there 如果你真的真的检查了jsfiddle,你可以在那里看到一些css

  1. to add some margin so we can see the tooltip that defaults to the top of the button 添加一些余量,以便我们可以看到默认为按钮顶部的工具提示

2.css that doesn't let button block mouse events from reaching the wrapper 2.css不允许按钮阻止鼠标事件到达包装器

#disabled-button-wrapper {
  display: inline-block; /* display: block works as well */
  margin: 50px; /* make some space so the tooltip is visible */
}

#disabled-button-wrapper  .btn[disabled] {
  /* don't let button block mouse events from reaching wrapper */
  pointer-events: none;
}

demo: https://jsfiddle.net/dLt2ed5w/6/ 演示: https//jsfiddle.net/dLt2ed5w/6/

You need below CSS. 你需要在CSS下面。

#disabled-button-wrapper .btn[disabled] {
  /* you need this line, not to block hover event from div */
  pointer-events: none;
}
#disabled-button-wrapper {
  display: inline-block; /* display: block works as well */
  margin: 50px; /* make some space so the tooltip is visible */
}

Explanation:- 说明:-

You need to set pointer-events: none; 你需要设置pointer-events: none; for the disabled button, beacuse disable button actually blocks the event from its parent container, so you need to tell not to block the hover event comming from container. 对于禁用按钮,beacuse禁用按钮实际上阻止事件从其父容器,所以你需要告诉不要阻止从容器传入的悬停事件。

Demo 演示

How to enable bootstrap tooltip on disabled button? 如何在禁用按钮上启用bootstrap工具提示?

 $(function() { $('[data-toggle="tooltip"]').tooltip(); }); 
 #disabled-button-wrapper { display: inline-block; } #disabled-button-wrapper .btn[disabled] { pointer-events: none; } #disabled-button-wrapper { cursor: not-allowed; } 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <div id="disabled-button-wrapper" data-placement="right" data-toggle="tooltip" data-title="Tooltip title"> <button class="btn btn-primary" disabled>Button</button> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM