开发者

What's wrong the this jquery plugin?

开发者 https://www.devze.com 2023-04-04 15:32 出处:网络
jQuery.fn.func = function() { this.each(function(){ $(this).onclick(function(){ alert($(this).attr(\'id\'));
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

精彩评论

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