jQuery vs document.querySelectorAll

后端 未结 12 923
闹比i
闹比i 2020-11-28 01:28

I heard several times that jQuery\'s strongest asset is the way it queries and manipulates elements in the DOM: you can use CSS queries to create complex queries that would

相关标签:
12条回答
  • 2020-11-28 01:37

    Just a comment on this, when using material design lite, jquery selector does not return the property for material design for some reason.

    For:

    <div class="logonfield mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
            <input class="mdl-textfield__input" type="text" id="myinputfield" required>
            <label class="mdl-textfield__label" for="myinputfield">Enter something..</label>
          </div>
    

    This works:

    document.querySelector('#myinputfield').parentNode.MaterialTextfield.change();
    

    This does not:

    $('#myinputfield').parentNode.MaterialTextfield.change();
    
    0 讨论(0)
  • 2020-11-28 01:40

    To understand why jQuery is so popular, it's important to understand where we're coming from!

    About a decade ago, top browsers were IE6, Netscape 8 and Firefox 1.5. Back in those days, there were little cross-browser ways to select an element from the DOM besides Document.getElementById().

    So, when jQuery was released back in 2006, it was pretty revolutionary. Back then, jQuery set the standard for how to easily select / change HTML elements and trigger events, because its flexibility and browser support were unprecedented.

    Now, more than a decade later, a lot of features that made jQuery so popular have become included in the javaScript standard:

    • Instead of jQuery's $(), you can now now use Document.querySelectorAll()
    • Instead of jQuery's $el.on(), you can now use EventTarget.addEventListener()
    • Instead of jQuery's $el.toggleClass(), you can now use Element.classList.toggle()
    • ...

    These weren't generally available back in 2005. The fact that they are today obviously begs the question of why we should use jQuery at all. And indeed, people are increasingly wondering whether we should use jQuery at all.

    So, if you think you understand JavaScript well enough to do without jQuery, please do! Don't feel forced to use jQuery, just because so many others are doing it!

    0 讨论(0)
  • 2020-11-28 01:45

    That's because jQuery can do much more than querySelectorAll.

    First of all, jQuery (and Sizzle, in particular), works for older browsers like IE7-8 that doesn't support CSS2.1-3 selectors.

    Plus, Sizzle (which is the selector engine behind jQuery) offers you a lot of more advanced selector instruments, like the :selected pseudo-class, an advanced :not() selector, a more complex syntax like in $("> .children") and so on.

    And it does it cross-browsers, flawlessly, offering all that jQuery can offer (plugins and APIs).

    Yes, if you think you can rely on simple class and id selectors, jQuery is too much for you, and you'd be paying an exaggerated pay-off. But if you don't, and want to take advantage of all jQuery goodness, then use it.

    0 讨论(0)
  • 2020-11-28 01:45

    Old question, but half a decade later, it’s worth revisiting. Here I am only discussing the selector aspect of jQuery.

    document.querySelector[All] is supported by all current browsers, down to IE8, so compatibility is no longer an issue. I have also found no performance issues to speak of (it was supposed to be slower than document.getElementById, but my own testing suggests that it’s slightly faster).

    Therefore when it comes to manipulating an element directly, it is to be preferred over jQuery.

    For example:

    var element=document.querySelector('h1');
    element.innerHTML='Hello';
    

    is vastly superior to:

    var $element=$('h1');
    $element.html('hello');
    

    In order to do anything at all, jQuery has to run through a hundred lines of code (I once traced through code such as the above to see what jQuery was actually doing with it). This is clearly a waste of everyone’s time.

    The other significant cost of jQuery is the fact that it wraps everything inside a new jQuery object. This overhead is particularly wasteful if you need to unwrap the object again or to use one of the object methods to deal with properties which are already exposed on the original element.

    Where jQuery has an advantage, however, is in how it handles collections. If the requirement is to set properties of multiple elements, jQuery has a built-in each method which allows something like this:

    var $elements=$('h2');  //  multiple elements
    $elements.html('hello');
    

    To do so with Vanilla JavaScript would require something like this:

    var elements=document.querySelectorAll('h2');
    elements.forEach(function(e) {
        e.innerHTML='Hello';
    });
    

    which some find daunting.

    jQuery selectors are also slightly different, but modern browsers (excluding IE8) won’t get much benefit.

    As a rule, I caution against using jQuery for new projects:

    • jQuery is an external library adding to the overhead of the project, and to your dependency on third parties.
    • jQuery function is very expensive, processing-wise.
    • jQuery imposes a methodology which needs to be learned and may compete with other aspects of your code.
    • jQuery is slow to expose new features in JavaScript.

    If none of the above matters, then do what you will. However, jQuery is no longer as important to cross-platform development as it used to be, as modern JavaScript and CSS go a lot further than they used to.

    This makes no mention of other features of jQuery. However, I think that they, too, need a closer look.

    0 讨论(0)
  • 2020-11-28 01:46

    I think the true answer is that jQuery was developed long before querySelector/querySelectorAll became available in all major browsers.

    Initial release of jQuery was in 2006. In fact, even jQuery was not the first which implemented CSS selectors.

    IE was the last browser to implement querySelector/querySelectorAll. Its 8th version was released in 2009.

    So now, DOM elements selectors is not the strongest point of jQuery anymore. However, it still has a lot of goodies up its sleeve, like shortcuts to change element's css and html content, animations, events binding, ajax.

    0 讨论(0)
  • 2020-11-28 01:47

    Here's a comparison if I want to apply the same attribute, e.g. hide all elements of class "my-class". This is one reason to use jQuery.

    jQuery:

    $('.my-class').hide();
    

    JavaScript:

    var cls = document.querySelectorAll('.my-class');
    for (var i = 0; i < cls.length; i++) {
        cls[i].style.display = 'none';
    }
    

    With jQuery already so popular, they should have made document.querySelector() behave just like $(). Instead, document.querySelector() only selects the first matching element which makes it only halfway useful.

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