开发者

jQuery get object moused over

开发者 https://www.devze.com 2023-02-22 14:49 出处:网络
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 elemen

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

精彩评论

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