开发者

What's wrong the this jquery plugin?

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜