开发者

Get <div> name using jQuery

开发者 https://www.devze.com 2023-03-22 17:32 出处:网络
$(\"body\").click(function() { var item = \"<h1>You clicked on: #\" + $(this).attr(\'id\'); $(\".position\").html(item开发者_如何学运维);
$("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)

0

精彩评论

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