我有一个从标题创建工具提示的功能。我试图将此函数包装在一个 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();
});
}
});