jQuery.click() vs onClick

前端 未结 17 1495
情话喂你
情话喂你 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:19

    The first method is to prefer. It uses the advanced event registration model[s], which means you can attach multiple handlers to the same element. You can easily access the event object, and the handler can live in any function's scope. Also, it is dynamic, i.e it can be invoked at any time and is especially well-suited for dynamically generated elements. Whether you use jQuery, an other library or the native methods directly does not really matter.

    The second method, using inline attributes, needs a lot of global functions (which leads to namespace pollution) and mixes the content/structure (HTML) with the behavior (JavaScript). Do not use that.

    Your question about performance or standards can't be easily answered. The two methods are just completely different, and do different things. The first one is mightier, while the second one is despised (considered bad style).

    0 讨论(0)
  • 2020-11-21 08:20

    You could combine them, use jQuery to bind the function to the click

    <div id="myDiv">Some Content</div>
    
    $('#myDiv').click(divFunction);
    
    function divFunction(){
     //some code
    }
    
    0 讨论(0)
  • 2020-11-21 08:20

    Most of the time, native JavaScript methods are a better choice over jQuery when performance is the only criteria, but jQuery makes use of JavaScript and makes the development easy. You can use jQuery as it does not degrade performance too much. In your specific case, the difference of performance is ignorable.

    0 讨论(0)
  • 2020-11-21 08:23

    Using $('#myDiv').click(function(){ is better as it follows standard event registration model. (jQuery internally uses addEventListener and attachEvent).

    Basically registering an event in modern way is the unobtrusive way of handling events. Also to register more than one event listener for the target you can call addEventListener() for the same target.

    var myEl = document.getElementById('myelement');
    
    myEl.addEventListener('click', function() {
        alert('Hello world');
    }, false);
    
    myEl.addEventListener('click', function() {
        alert('Hello world again!!!');
    }, false);
    

    http://jsfiddle.net/aj55x/1/

    Why use addEventListener? (From MDN)

    addEventListener is the way to register an event listener as specified in W3C DOM. Its benefits are as follows:

    • It allows adding more than a single handler for an event. This is particularly useful for DHTML libraries or Mozilla extensions that need to work well even if other libraries/extensions are used.
    • It gives you finer-grained control of the phase when the listener gets activated (capturing vs. bubbling)
    • It works on any DOM element, not just HTML elements.

    More about Modern event registration -> http://www.quirksmode.org/js/events_advanced.html

    Other methods such as setting the HTML attributes, example:

    <button onclick="alert('Hello world!')">
    

    Or DOM element properties, example:

    myEl.onclick = function(event){alert('Hello world');}; 
    

    are old and they can be over written easily.

    HTML attribute should be avoided as It makes the markup bigger and less readable. Concerns of content/structure and behavior are not well-separated, making a bug harder to find.

    The problem with the DOM element properties method is that only one event handler can be bound to an element per event.

    More about Traditional event handling -> http://www.quirksmode.org/js/events_tradmod.html

    MDN Reference: https://developer.mozilla.org/en-US/docs/DOM/event

    0 讨论(0)
  • 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

    <span id="JQueryClick">Click #JQuery</span> </br>
    <span id="JQueryAttrClick">Click #Attr</span> </br>
    

    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.

    0 讨论(0)
提交回复
热议问题