开发者

How to access a calling element's id in a jQuery click() function

开发者 https://www.devze.com 2023-03-20 13:48 出处:网络
So I am binding a .click() function to a css class, but I want to b开发者_如何学Goe able to access the calling element\'s id from inside this function when it is called. What is the best way to do thi

So I am binding a .click() function to a css class, but I want to b开发者_如何学Goe able to access the calling element's id from inside this function when it is called. What is the best way to do this?


You can pull it right out of this:

$(selector).click(function() {
    var id = this.id;
    // Do interesting things.
});

The this in a jQuery click callback is just a DOM object that exposes its attributes as object properties.


You can use the this key word:

$(".elementClass").click(function () {
   var currentElement = $(this); // refers to the current element

   var theId = currentElement.attr("id");
});
0

精彩评论

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