Is it possible to show a tooltip without making a link?
For example, I have the following code without a link:
<ul 开发者_JAVA百科class="letters" title="This is the title">
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
So how can I show the tooltip "This is the title" when I mouse over on it?
Btw, I don't want to use any tooltip plugin.
You're making your life a bit harder saying you don't want to take advantage of any work that other people have already done. :-)
Some of the component parts:
You can find all of the
ul
elements that have atitle
attribute via$
orjQuery
:var targets = $("ul[title]");
You can watch them for the
mouseenter
event and, if nomouseleave
event fires within X milliseconds, show your tooltip. (These are IE-specific events, but jQuery provides them on all browsers, which is very helpful. It also provideshover
, which is just a convenience means of hooking upmouseenter
andmouseleave
handlers.) Although this means you're hooking up a lot of event handlers (sincemouseenter
andmouseleave
don't bubble — part of what makes them so handy). You may well be better off trackingmouseover
/mouseout
at the document level and handling the complexities that arise, since they do bubble.To show your tooltip, you can get the location of the element in question (via
offset
) and show an absolutely-positioneddiv
nearby containing the title (which you can get viaattr
).When the user moves the mouse out of the tooltip (
mouseleave
), remove it.
But again, you might consider using the code from a project that's already done this, found all the wrinkles, etc. Read through the code first to make sure it's doing what you want and not something you don't want, but reinventing the wheel is usually (not always!) a waste of time... Even if you don't actually end up using the plug-in, reading through to see what the pitfalls are might be useful.
Get the title attribute
title = $('.letters').attr('title');
And work it from there. There's no magical "title_tooltip_popup()" function in Jquery, so you might consider making your own or using a plugin.
Remember that if you're working with classes this selector
$('.letters')
might reference more than 1 element
精彩评论