问题
I want to add a tooltip in a text so for example if I have a code like this:
<a href="http://google.com" title="The World's Largest Search Engine!">Google!</a>
On mouse hover, I want to show that tooltip.
Using title
is a good way but how can I style to look it better? Any help:
JSFIDDLE
回答1:
If you wish to create it dynamically using jquery, you may do the following :
<a href="http://google.com" data-title="The World's Largest Search Engine!">Google!</a>
<style>
.box
{
border: 1px solid green;
position:absolute;
color: white;
top: 19px;
left: 30px;
background-color: black;
}
</style>
<script>
$().ready(function(){
$( "a" ).hover(
function() {
var title = $(this).attr("data-title"); // extracts the title using the data-title attr applied to the 'a' tag
$('<div/>', { // creates a dynamic div element on the fly
text: title,
class: 'box'
}).appendTo(this); // append to 'a' element
}, function() {
$(document).find("div.box").remove(); // on hover out, finds the dynamic element and removes it.
}
);
});
</script>
Example : http://jsfiddle.net/9RxLM/5164/
来源:https://stackoverflow.com/questions/34278213/add-tooltip-in-a-anchor-link