[英]How to enable tooltip on disabled button in javascript?
我有一個禁用的按鈕,我希望它顯示一個工具提示,但是當我再次啟用它時更改它。 我如何讓它工作我只能使用 HTML、javascript 和 css。 這是按鈕
<div class="right-item"><button class="button ripple" id="print" onclick="printDiv('qr-code')" disabled="disabled">Drucken</div>
<button disabled title="Hello">Hover on me</button>
此代碼將為禁用或啟用按鈕顯示相同的工具提示。 有一個 function 檢查按鈕的狀態並基於它應用標題值。
使用標題
function toggle() { var button = document.getElementById('button'); if (button.disabled) { button.disabled = false; button.setAttribute('title', 'Enabled title'); } else { button.disabled = true; button.setAttribute('title', 'Disabled title'); } }
<button onclick="toggle()">Toggle enable/disable</button> <button disabled title="Enabled title" id="button">Hover on me</button>
啟用按鈕時需要添加 Javascript。
在 jQuery 中:
$('#yourElementId').attr('title', 'your new title');
在 Javascript 中:
var myBtn= document.getElementById("yourElementId");
myBtn.setAttribute('title','mytitle');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.