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");
});
精彩评论