简体   繁体   English

如何在 javascript 中的禁用按钮上启用工具提示?

[英]How to enable tooltip on disabled button in javascript?

I have a disabled button and i want it to show a tooltip but change it when i enable it again.我有一个禁用的按钮,我希望它显示一个工具提示,但是当我再次启用它时更改它。 How do i get this to work i can only use HTML, javascript and css.我如何让它工作我只能使用 HTML、javascript 和 css。 This is the button这是按钮

 <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>

This code will show same tool tip for both disabled or enabled button.此代码将为禁用或启用按钮显示相同的工具提示。 Have a function which checks the status of button and apply the title value based on it.有一个 function 检查按钮的状态并基于它应用标题值。

Use title使用标题

 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>

You need to add Javascript when you enable button.启用按钮时需要添加 Javascript。

In jQuery:在 jQuery 中:

$('#yourElementId').attr('title', 'your new title');

In Javascript:在 Javascript 中:

var myBtn= document.getElementById("yourElementId");
myBtn.setAttribute('title','mytitle');

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

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