如何使用 jQuery 显示悬停时的工具提示消息?

如标题所述,我如何使用 jQuery 显示悬停时的工具提示消息?

294525 次浏览

take a look at the jQuery Tooltip plugin. You can pass in an options object for different options.

There are also other alternative tooltip plugins available, of which a few are

Take look at the demos and documentation and please update your question if you have specific questions about how to use them in your code.

Tooltip plugin might be too heavyweight for what you need. Simply set the 'title' attribute with the text you desire to show in your tooltip.

$("#yourElement").attr('title', 'This is the hover-over text');

As recommended qTip and other projects are quite old I recommend using qTip2 as it is most up-to-date.

You can use bootstrap tooltip. Do not forget to initialize it.

<span class="tooltip-r" data-toggle="tooltip" data-placement="left" title="Explanation">
inside span
</span>

Will be shown text Explanation on the left side.

and run it with js:

$('.tooltip-r').tooltip();

Take a look at ToolTipster

  • easy to use
  • flexible
  • pretty lightweight, compared to some other tooltip plugins (39kB)
  • looks better, without additional styling
  • has a good set of predefined themes

Following will work like a charm (assuming you have div/span/table/tr/td/etc with "id"="myId")

    $("#myId").hover(function() {
$(this).css('cursor','pointer').attr('title', 'This is a hover text.');
}, function() {
$(this).css('cursor','auto');
});

As a complimentary, .css('cursor','pointer') will change the mouse pointer on hover.

You can do it using just css without using any jQiuery.

<a class="tooltips">
Hover Me
<span>My Tooltip Text</span>
</a>
<style>
a.tooltips {
position: relative;
display: inline;
}


a.tooltips span {
position: absolute;
width: 200px;
color: #FFFFFF;
background: #000000;
height: 30px;
line-height: 30px;
text-align: center;
visibility: hidden;
border-radius: 6px;
}


a.tooltips span:after {
content: '';
position: absolute;
top: 100%;
left: 35%;
margin-left: -8px;
width: 0;
height: 0;
border-top: 8px solid #000000;
border-right: 8px solid transparent;
border-left: 8px solid transparent;
}


a:hover.tooltips span {
visibility: visible;
opacity: 0.8;
bottom: 30px;
left: 50%;
margin-left: -76px;
z-index: 999;
}
</style>

For Latest/Up to date solution you may try Tippy.js it's the complete tooltip, popover, dropdown, and menu solution for the web, powered by Popper.

Tippy.js

As mentioned in most of the answers the uses of plugins would be heavy compared with the requirements here. which is why here's another solution to get the required output just with simple HTML, CSS & JQuery code.

using CSS will give us a better view, check the code below

$(".tool-tip").attr('title-new', 'another example to display the hover-over texts');
button {
background: aqua;
padding:10px;
}
.tool-tip[title-new]:hover:after {
content: attr(title-new);
position: absolute;
border: #c0c0c0 1px dotted;
padding: 10px;
display: block;
z-index: 100;
background-color: #000000;
color: #ffffff;
max-width: 200px;
text-decoration: none;
text-align:center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>hover on the button below to check the results</p>
<button class="tool-tip">Here I am</button>