jQuery.fn.func = function()
{
this.each(function(){
$(this).onclick(function(){
alert($(this).attr('id'));
});
});
}
$('#id1,#id开发者_如何学C2').func();
It reports:
$(this).onclick is not a function.
What's wrong?
jQuery does not define a method called onclick
- it is click
.
$(this).click(function()
{
// Do Stuff
});
Replace onclick
with click
You will find the documentation of this event method on a dedicated jQuery doc page.
.click( handler(eventObject) )
Description: Bind an event handler to the "click" JavaScript event, or trigger that event on an element
The right way is:
$(this).click(function(){
alert($(this).attr('id'));
});
Use .click
instead of .onclick
.
精彩评论