我需要在禁用按钮上显示工具提示,并在启用按钮上删除它。目前,它是反向工作的。

扭转这种行为的最好方法是什么?

$('[rel=tooltip]').tooltip(); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <hr> <button class="btn" disabled rel="tooltip" data-title="Dieser Link führt zu Google">button disabled</button> <button class="btn" rel="tooltip" data-title="Dieser Link führt zu Google">button not disabled</button>

这是一个演示

附注:我想保留disabled属性。


当前回答

你可以简单地覆盖Bootstrap的“指针事件”样式通过CSS禁用按钮。

.btn[disabled] {
 pointer-events: all !important;
}

最好还是显式地禁用特定的按钮。

#buttonId[disabled] {
 pointer-events: all !important;
}

其他回答

你可以包装禁用按钮,并将工具提示放在包装器上:

<div class="tooltip-wrapper" data-title="Dieser Link führt zu Google">
  <button class="btn btn-default" disabled>button disabled</button>
</div>

如果包装器有display:inline,那么工具提示似乎不起作用。使用display:block和display:inline-block似乎工作得很好。它似乎也可以很好地使用浮动包装器。

这是一个更新的JSFiddle,它与最新的Bootstrap(3.3.6)一起工作。感谢@JohnLehmann建议指针事件:没有;为禁用按钮。

http://jsfiddle.net/cSSUA/209/

只需覆盖css禁用悬停事件在你的元素

添加css:

pointer-events: all !important;
cursor: not-allowed !important;

这将禁用点击按钮,但仍将允许悬停事件使工具提示工作

只需将禁用类(而不是disabled属性)添加到按钮,即可使其可见地禁用。

<button class="btn disabled" rel="tooltip" data-title="Dieser Link führt zu Google">button disabled</button>

注意:这个按钮只是看起来是禁用的,但它仍然会触发事件,你只需要注意这一点。

Bootstrap 3.3.6的工作代码

Javascript:

$('body').tooltip({
  selector: '[data-toggle="tooltip"]'
});

$(".btn").click(function(e) {
 if ($(this).hasClass("disabled")){
   e.preventDefault();
 }
});

CSS:

a.btn.disabled, fieldset[disabled] a.btn {
  pointer-events: auto;
}

如果你(像我一样)迫切需要复选框、文本框之类的工具提示,那么这里是我的破解方法:

$('input:disabled, button:disabled').after(function (e) {
    d = $("<div>");
    i = $(this);
    d.css({
        height: i.outerHeight(),
        width: i.outerWidth(),
        position: "absolute",
    })
    d.css(i.offset());
    d.attr("title", i.attr("title"));
    d.tooltip();
    return d;
});

工作示例:http://jsfiddle.net/WB6bM/11/

无论如何,我相信禁用表单元素的工具提示对用户体验非常重要。如果你阻止某人做某事,你应该告诉他们原因。