
Originally Posted by
Pullo
Could you post a bit of code (i.e. JS, CSS, HTML), so that I can have a look.
Sure. Here is my working example, prior to your and jackleaves' examples:
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Tooltip JS</title>
<style>
a {position: relative;}
a span {position: absolute; left:0; top: 100%; display: block; padding: 10px; background: black; color: white; width: 200px;}
</style>
</head>
<body>
<h1>Tooltip JS</h1>
<p>This is a paragraph with links to sites like <a href="" title="Wikipedia is a free encyclopedia maintained by the public.">Wikipedia</a>, the magnificent <a href="" title="The SitePoint networks includes articles, learning resources and awesome forums">SitePoint</a>, and our favorite site <a href="" title="A List Apart posts regular articles for people who makes websites.">A List Apart</a>.</p>
<p>More text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text.</p>
<script type="text/javascript">
var Tooltips =
{
init: function() // event listener function
{
var links = document.getElementsByTagName("a");
for (i=0, ii=links.length; i<ii; i++)
{
if(links[i].title && links[i].title.length > 0)
{
links[i].addEventListener("mouseover",Tooltips.showTipListener, false);
links[i].addEventListener("focus",Tooltips.showTipListener, false);
links[i].addEventListener("mouseout",Tooltips.hideTipListener, false);
links[i].addEventListener("blur",Tooltips.hideTipListener, false);
}
}
},
showTipListener: function(e)
{
Tooltips.showTip(this);
e.preventDefault();
},
hideTipListener: function(e)
{
Tooltips.hideTip(this);
e.preventDefault();
},
showTip: function(link) // what happens when the listener detects mouseover or focus event
{
if (!link.title == "")
{
var span = document.createElement("span");
var tipText = document.createTextNode(link.title);
span.appendChild(tipText);
link.appendChild(span);
link.title = "";
}
},
hideTip: function(link) // what happens when the listener detects mouseout or blur event
{
if (link.title == "")
{
var tip = link.lastChild; // really not sure if that will work to target the span
link.title = tip.firstChild.nodeValue;
link.removeChild(tip);
}
}
};
Tooltips.init();
</script>
</body>
</html>
You'll see that I started with a workaround for the issue in this thread. Instead of checking for the presence of a <span>, I just check to see if the <a>'s title element is empty:
Code:
if (!link.title == "")
I'm pleased I found a way to make it work, but I'd prefer to check if the span is there or not.
You can see the need for this by hovering over a link and then tabbing to it. Without the code above, an extra, empty <span> is created when the link has focus. (It's a small point, but an interesting issue. It could be solved with CSS, of course—by hiding span + span—but as I said, this is just a JS learning exercise for me.)
Bookmarks