我有一个从标题创建工具提示的功能。我试图在返回工具提示前检查标题长度的if语句中封装这个函数。但它似乎不想返回任何东西。 if语句有没有更好的方法来做到这一点?来自标题属性的自定义工具提示功能
$(document).ready(function() {
if ($('#menu-headermenu a').attr('title').length == 0) {
return null;
} else {
//Select all anchor tag with rel set to tooltip
$('#menu-headermenu a').mouseover(function(e) {
//Grab the title attribute's value and assign it to a variable
var tip = $(this).attr('title');
//Remove the title attribute's to avoid the native tooltip from the browser
$(this).attr('title','');
//Append the tooltip template and its value
$(this).append('<div id="tooltip"><p>Take me to:<p><div class="tooltipcontent"' + tip + '</div></div>');
//Set the X and Y axis of the tooltip
$('#tooltip').css('top', e.pageY + 10);
$('#tooltip').css('left', e.pageX + 20);
//Show the tooltip with faceIn effect
$('#tooltip').fadeIn('500');
$('#tooltip').fadeTo('10',0.8);
}).mousemove(function(e) {
//Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
$('#tooltip').css('top', e.pageY + 10);
$('#tooltip').css('left', e.pageX + 20);
}).mouseout(function() {
//Put back the title attribute's value
$(this).attr('title',$('div.tooltipcontent').html());
//Remove the appended tooltip template
$(this).children('div#tooltip').remove();
});
}
});
我喜欢那个选择器过滤器;不会将它添加到我的答案中,因为信用欠额,但也可能与我的方法混在一起。 –
哇,我很抱歉,我首先查看了这个答案。我不太熟悉过滤器。这完全是我所需要的。 非常感谢----你们所有人! – patrick