开发者

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)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜