Native JavaScript implements jquery code effect comparison

Source: Internet
Author: User

Here is the equivalent code for jquery and JavaScript for the same operation.

Select Element

JavaScript code
  1. Jquery
  2. var els = $ ('. el ');
  3. Native methods
  4. var els = document.queryselectorall ('. el ');
  5. Function method
  6. var $ = function (el) {
  7. return Document.queryselectorall (el);
  8. }
  9. var els = $ ('. el ');
  10. or use regex-based micro-selector library, address: http://jsperf.com/micro-selector-libraries



Creating Elements

JavaScript code
    1. Jquery
    2. var newel = $ (' <div/> ');
    3. Native methods
    4. var newel = document.createelement (' div ');



Adding event listeners

JavaScript code
    1. Jquery
    2. $ ('. el '). on (' event ', function () {
    3. });
    4. Native methods
    5. [].foreach.call (document.queryselectorall ('. el '), function (el) {
    6. El.addeventlistener (' event ', function () {
    7. }, false);
    8. });



Set/get Properties

JavaScript code
    1. Jquery
    2. $ ('. El '). filter (': first '). attr ('key ', ' value ');
    3. $ ('. El '). filter (': first '). attr ('key ');
    4. Native methods
    5. Document.queryselector ('. el '). setAttribute ('key ', ' value ');
    6. Document.queryselector ('. el '). getattribute ('key ');



Add/remove/toggle Classes

JavaScript code
    1. Jquery
    2. $ ('. el '). addclass ('class ');
    3. $ ('. el '). removeclass ('class ');
    4. $ ('. el '). toggleclass ('class ');
    5. Native methods
    6. Document.queryselector ('. el '). classlist.add ('class ');
    7. Document.queryselector ('. el '). classlist.remove ('class ');
    8. Document.queryselector ('. el '). classlist.toggle ('class ');



Additional content (Append)

JavaScript code
    1. Jquery
    2. $ ('. el '). append ($ ('<div/> '));
    3. Native methods
    4. Document.queryselector ('. el '). appendchild (document.createelement (' div '));



Cloning elements

JavaScript code
    1. Jquery
    2. var Clonedel = $ ('. el '). clone ();
    3. Native methods
    4. var Clonedel = document.queryselector ('. el '). cloneNode (true);



remove Element

JavaScript code
    1. Jquery
    2. $ ('. el '). Remove ();
    3. Native methods
    4. Remove ('. el ');
    5. function Remove (el) {
    6. var toremove = Document.queryselector (el);
    7. ToRemove.parentNode.removeChild (toremove);
    8. }



Get parent element

JavaScript code
    1. Jquery
    2. $ ('. el '). Parent ();
    3. Native methods
    4. Document.queryselector ('. el '). parentnode;



previous/next Element

JavaScript code
    1. Jquery
    2. $ ('. el '). prev ();
    3. $ ('. el '). next ();
    4. Native methods
    5. Document.queryselector ('. el '). previouselementsibling;
    6. Document.queryselector ('. el '). nextelementsibling;



XHR or Ajax

JavaScript code
    1. Jquery
    2. $.get (' url ', function (data) {
    3. });
    4. $.post (' url ', {data:data}, function (data) {
    5. });
    6. Native methods
    7. Get
    8. var xhr = new XMLHttpRequest ();
    9. Xhr.open (' GET ', url);
    10. Xhr.onreadystatechange = function (data) {
    11. }
    12. Xhr.send ();
    13. Post
    14. var xhr = new XMLHttpRequest ()
    15. Xhr.open (' POST ', url);
    16. Xhr.onreadystatechange = function (data) {
    17. }
    18. Xhr.send ({data:data});

Native JavaScript implements the jquery code effect comparison

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.