是否有可能使锚标记的标题属性在鼠标悬停时立即出现,而不是在几秒钟后出现。 JavaScript/jQuery 和 CSS 中的任何解决方案都很好。
title
属性的处理取决于浏览器,并且没有定义 API 来控制它,规范中也没有具体说明。这包括延迟、显示的持续时间、使用的字体、字体大小等
可以使用其他技术来代替title
属性。其中一些在其他答案中已经提到过。请注意,简单的“CSS 工具提示”可以在纯 CSS 中相当简单和灵活地实现。使用这些技术时,要显示的数据通常是not 在
title
属性中,因为它的处理取决于浏览器,因此存在让您的特殊工具提示显示和 浏览器的 实现的风险
title
。尽管可以防止后者,但在启用脚本时,使用对任何内容都没有默认效果的属性会更简单,例如
data-title=...
或
data-tooltip=...
。
// textFrom : String, the attribute from which the text
// should come,
// delta : String or Number, the distance from the cursor at
// which the tooltip should appear
function instantTooltips(textFrom, delta) {
// if delta exists, and can be parsed to a number, we use it,
// otherwise we use the default of 5:
delta = parseFloat(delta) ? parseFloat(delta) : 5;
// function to handle repositioning of the created tooltip:
function reposition(e) {
// get the tooltip element:
var tooltip = this.nextSibling;
// setting the position according to the position of the
// pointer:
tooltip.style.top = (e.pageY + delta) + 'px';
tooltip.style.left = (e.pageX + delta) + 'px';
}
// get all elements that have an attribute from which we
// want to get the tooltip text from:
var toTitle = document.querySelectorAll('[' + textFrom + ']'),
//create a span element:
span = document.createElement('span'),
// find if we should use textContent or innerText (IE):
textProp = 'textContent' in document ? 'textContent' : 'innerText',
// caching variables for use in the upcoming forEach:
parent, spanClone;
// adding a class-name (for CSS styling):
span.classList.add('createdTooltip');
// iterating over each of the elements with a title attribute:
[].forEach.call(toTitle, function(elem) {
// reference to the element's parentNode:
parent = elem.parentNode;
// cloning the span, to avoid creating multiple elements:
spanClone = span.cloneNode();
// setting the text of the cloned span to the text
// of the attribute from which the text should be taken:
spanClone[textProp] = elem.getAttribute(textFrom);
// inserting the created/cloned span into the
// document, after the element:
parent.insertBefore(spanClone, elem.nextSibling);
// binding the reposition function to the mousemove
// event:
elem.addEventListener('mousemove', reposition);
// we're setting textFrom attribute to an empty string
// so that the CSS will still apply, but which
// shouldl still not be shown by the browser:
elem.setAttribute(textFrom, '');
});
}
// calling the function:
instantTooltips('title', 15);
[title] {
display: block;
margin: 0 0 1em 0;
}
/*
hiding, and styling, the elements we'll be creating
*/
[title] + span.createdTooltip {
display: none;
border: 2px solid #f90;
background-color: rgba(255, 255, 255, 0.8);
padding: 0.2em 0.5em;
border-radius: 0.75em;
}
/*
showing the created elements on hovering the element we want to
show tooltips for
*/
[title]:hover + span.createdTooltip {
display: block;
position: absolute;
}
<span title="This is the span's title">A span element</span>
<img src="http://placekitten.com/g/250/250" title="A kitten." />
<input title="This is an input element's title." value="This input has a title" />
参考资料:
mouseover
上的标题并附加一个跨度。然后删除跨度并恢复标题
mouseout
$('a').hover(function(e){
title = $(this).attr('title');
$(this).append('<span>Im Super Fast!!!</span>')
$(this).removeAttr('title');
},
function(e){
$('span', this).remove();
$(this).attr('title',title);
});
检查示例 -
http://jsfiddle.net/1z3catx3/1/
注意:您当然需要设置span
<!DOCTYPE html>
<html>
<head>
<style>
[data-title]:hover:after {
opacity: 1;
transition: all 0.1s ease 0.5s;
visibility: visible;
}
[data-title]:after {
content: attr(data-title);
position: absolute;
bottom: -1.6em;
z-index: 99999;
visibility: hidden;
white-space: nowrap;
background-color: lightgray;
color: #111;
font-size: 90%;
font-weight: lighter;
padding: 1px 5px 2px 5px;
box-shadow: 1px 1px 3px #222222;
opacity: 0;
border: 1px solid #111111;
border-radius: 5px 5px 5px 5px;
}
[data-title] {
position: relative;
}
</style>
</head>
<body>
<p data-title="also you can put it here for toltip other element ">my element </p>
</body>
</html>