开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜