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();
});
精彩评论