开发者

jQuery get object moused over

I have this code:

    $('*').mouseover(function() {
        $('#log').text($('*').id);
    });

When you mouse over any element on开发者_StackOverflow中文版 the page, I want #log to have the id of that element. Obviously the code above doesn't work... How do I do this?


$('*').mouseover(function() {
    console.log($(this).attr('id'))
});

In almost all jQuery callbacks, "this" is the object on which the callback is being executed.


$('*').mouseover(function() {
    $('#log').text($(this).attr('id'));
});


You can also use event.target

var $log = $("#log");
$('*').mouseover(function(event) {
    $log.text($(event.target).attr('id'));
    event.stopPropagation();
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜