How does this jQuery selector work?

I am writing a simple hint:

$(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(); 
});

});

As you can see, the div with the identifier "toolTip" is dynamically added to the DOM when the user hovers over the link. This div does not exist initially when the DOM is loaded, but added later. Therefore, I suggested that I should use the function live()and attach an event to it. But some how it works if I just see it as a regular selector. Not that I complained, but how does it work?

+5
source share
2 answers

You do not need to live, because this selector does not start before the element is in the DOM.

  • Added OnMouseOver tooltip to DOM.
  • Onmouseout, , DOM. , mouseout ​​ DOM .

'live()', <a> DOM, . IE. , , .

+13

, .css() , , , , .

0

All Articles