我尝试了其他similar threads上的解决方案,但无法让它们工作。
我有一个引导按钮,当处于禁用状态时,应该有一个工具提示,让用户知道为什么它被禁用。
我知道一个处于禁用状态的按钮有一个CSS,它可以阻止鼠标悬停事件到达按钮元素,并且有一些解决方法。
我在下面的代码中做错了什么?
// Grab button from DOM
let resultsBtn = document.getElementById("get-results-btn");
// if conditional is met, disable button
$(resultsBtn).prop("disabled", true);
// then, add tooltip
$(resultsBtn).attr("data-toggle", "tooltip");
$(resultsBtn).attr("title", "you may only submit this form once");
// add css rules to make tooltip work on disabled button
$(resultsBtn).css("pointer-events", "none");
/* ====================================== */
/* Button styling */
/* ====================================== */
#get-results-btn {
// insert styling rules for color, size, etc. here, plus the following rules:
cursor: pointer;
user-select: none;
-webkit-user-select: none;
touch-action: manipulation;
pointer-events: none; ////// Added this based on [S/O thread][1]
}
#get-results-btn .btn[disabled] //// I pulled this line from [S/O thread][1], but I don't think the class applies to my code?
<button id="get-results-btn" class="btn standard-btn-style">
Get Results
</button>
2条答案
按热度按时间cgyqldqp1#
试试这个代码:
我对你的代码所做的修改:
pointer-events: none
。已更正。data-bs-toggle
而不是data-toggle
。4si2a6ki2#
根据文件:
具有
disabled
属性的元素不是交互式的,这意味着用户不能聚焦、悬停或单击它们来触发工具提示(或弹出框)。作为一种解决方法,您需要从 Package 器<div>
或<span>
触发工具提示,理想情况下使用tabindex="0"
使其可用于键盘聚焦,并覆盖禁用元素上的pointer-events。引用自文档本身的示例:
字符串