我需要在禁用按钮上显示工具提示,并在启用按钮上删除它。目前,它是反向工作的。
扭转这种行为的最好方法是什么?
$('[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属性。
无法在禁用按钮上显示工具提示。这是因为禁用的元素不会触发任何事件,包括工具提示。最好的方法是假装按钮已禁用(这样它的外观和行为就像已禁用一样),这样您就可以触发工具提示。
如。Javascript:
$('[rel=tooltip].disabled').tooltip();
$('[rel=tooltip].disabled').bind('click', function(){
return false;
});
而不仅仅是$('[rel=tooltip]').tooltip();
HTML:
<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>
JSFiddle: http://jsfiddle.net/BA4zM/75/
下面是一些工作代码:http://jsfiddle.net/mihaifm/W7XNU/200/
$('body').tooltip({
selector: '[rel="tooltip"]'
});
$(".btn").click(function(e) {
if (! $(this).hasClass("disabled"))
{
$(".disabled").removeClass("disabled").attr("rel", null);
$(this).addClass("disabled").attr("rel", "tooltip");
}
});
其思想是将工具提示添加到带有选择器选项的父元素中,然后在启用/禁用按钮时添加/删除rel属性。
引导3
HTML
<button
class="btn btn-success"
disabled
data-hint="Enabled">
<div class="sp-btn-overlap" data-hint="Disabled"></div>
Submit button
</button>
CSS
button { position:relative; }
.sp-btn-overlap {
position:absolute;
top:0;
left:0;
height:100%;
width:100%;
background:none;
border:none;
z-index:1900;
}
JS
$('button .sp-btn-overlap')
.popover({
trigger: 'hover',
container: 'body',
placement: 'bottom',
content: function() {
return $(this).parent().prop('disabled')
? $(this).data('hint')
: $(this).parent().data('hint');
}
});
$('button .sp-btn-overlap')
.popover({
trigger: 'hover',
container: 'body',
placement: 'bottom',
content: function() {
return $(this).parent().prop('disabled')
? $(this).data('hint')
: $(this).parent().data('hint'); }
});
button {
position:relative; /* important! */
}
.sp-btn-overlap {
position:absolute;
top:0;
left:0;
height:100%;
width:100%;
background:none;
border:none;
z-index:1900;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<button disabled class="btn btn-default" data-hint="Enabled">
<div class="sp-btn-overlap" data-hint="Disabled"></div>
Disabled button
</button>
<button class="btn btn-default" data-hint="Enabled">
<div class="sp-btn-overlap" data-hint="Disabled"></div>
Enabled button
</button>
这些变通办法是丑陋的。我已经调试的问题是,引导自动设置CSS属性指针事件:无禁用元素。
这个神奇的属性导致JS无法处理与CSS选择器匹配的元素上的任何事件。
如果你将这个属性重写为默认属性,所有的东西都会像魔法一样工作,包括工具提示!
.disabled {
pointer-events: all !important;
}
然而,你不应该使用这种通用的选择器,因为你可能不得不手动停止JavaScript事件传播的方式,你知道的(e.c preventdefault())。