Get <div> name using jQuery
$("body").click(function() {
var item = "<h1>You clicked on: #" + $(this).attr('id');
$(".position").html(item开发者_如何学运维);
});
Why is this giving me a blank response? I want it to identify each HTML object's ID clicked.
Try this
$("body").click(function(e) {
var item = "<h1>You clicked on: #" + $(e.target).attr('id') + "</h1>";
$(".position").html(item);
});
this
refers the the object that you registered the handler with, not the original source of the event.
In your case, this
is always the <body>
.
You want e.target
, which refers to the element that directly triggered the event.
(where e
is the first argument of the handler function)
精彩评论