jQuery.click() vs onClick

前端 未结 17 1746
情话喂你
情话喂你 2020-11-21 07:35

I have a huge jQuery application, and I\'m using the below two methods for click events.

First method

HTML

17条回答
  •  渐次进展
    2020-11-21 08:24

    Difference in works. If you use click(), you can add several functions, but if you use an attribute, only one function will be executed - the last one.

    DEMO

    HTML

    Click #JQuery 
    Click #Attr

    JavaScript

    $('#JQueryClick').click(function(){alert('1')})
    $('#JQueryClick').click(function(){alert('2')})
    
    $('#JQueryAttrClick').attr('onClick'," alert('1')" ) //This doesn't work
    $('#JQueryAttrClick').attr('onClick'," alert('2')" )
    

    If we are talking about performance, in any case directly using is always faster, but using of an attribute, you will be able to assign only one function.

提交回复
热议问题