我正在写一个简单的工具提示:
$(function() {
$('a').hover(function() {
var curLink = $(this);
var toolTipText = curLink.attr('title');
if (toolTipText)
{
var theOffset = curLink.offset();
$('body').prepend('<div id="toolTip">'+toolTipText+'</div>');
// how the heck is this working???
$('#toolTip').css({
'left' : theOffset.left+'px',
'top' : theOffset.top - 30+'px'
});
}
}, function() {
$('#toolTip').remove();
});
});
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,当用户将鼠标悬停在链接上时,会将ID为"toolTip"的div动态添加到DOM中.当DOM加载但稍后添加时,该div最初不存在.所以我假设我必须使用该live()函数并附加一个事件.但是,如果我将它视为常规选择器,它会如何工作.不是我在抱怨,但这是如何工作的?