Home  >  Article  >  Web Front-end  >  Detailed examples of 16 methods on how to replace JQuery with native js

Detailed examples of 16 methods on how to replace JQuery with native js

伊谢尔伦
伊谢尔伦Original
2017-06-17 11:46:351799browse

Bring you a simple implementation of native JS replacing some JQuery methods. I think it’s pretty good, so I’d like to share it with everyone and give it as a reference.

1. Select elements

// jQuery
var els = $('.el');

// Native
var els = document.querySelectorAll('.el');

// Shorthand
var $ = function (el) {
 return document.querySelectorAll(el);
}

querySelectorAll method returns a NodeList object, which needs to be converted into an array.

myList = Array.prototype.slice.call(myNodeList)

2.Create element

// jQuery
var newEl = $(&#39;<p/>&#39;);

// Native
var newEl = document.createElement(&#39;p&#39;);

3.Add event

// jQuery
$(&#39;.el&#39;).on(&#39;event&#39;, function() {

});

// Native
[].forEach.call(document.querySelectorAll(&#39;.el&#39;), function (el) {
 el.addEventListener(&#39;event&#39;, function() {

 }, false);
});

4.get/setAttributes

// jQuery
$(&#39;.el&#39;).filter(&#39;:first&#39;).attr(&#39;key&#39;, &#39;value&#39;);
$(&#39;.el&#39;).filter(&#39;:first&#39;).attr(&#39;key&#39;);

// Native
document.querySelector(&#39;.el&#39;).setAttribute(&#39;key&#39;, &#39;value&#39;);
document.querySelector(&#39;.el&#39;).getAttribute(&#39;key&#39;);

5. Add and remove styles Class

The DOM element itself has a readable and writable className attribute, which can be used to operate classes.

HTML 5 also provides a classList object with more powerful functions (not supported by IE 9).

// jQuery
$(&#39;.el&#39;).addClass(&#39;class&#39;);
$(&#39;.el&#39;).removeClass(&#39;class&#39;);
$(&#39;.el&#39;).toggleClass(&#39;class&#39;);

// Native
document.querySelector(&#39;.el&#39;).classList.add(&#39;class&#39;);
document.querySelector(&#39;.el&#39;).classList.remove(&#39;class&#39;);
document.querySelector(&#39;.el&#39;).classList.toggle(&#39;class&#39;);

6. Append elements

Append elements to the tail:

// jQuery
$(&#39;.el&#39;).append($(&#39;<p/>&#39;));

// Native
document.querySelector(&#39;.el&#39;).appendChild(document.createElement(&#39;p&#39;));

Append elements to the head:

//jQuery
$(‘.el&#39;).prepend(&#39;<p></p>&#39;)

//Native
var parent = document.querySelector(&#39;.el&#39;);
parent.insertBefore("<p></p>",parent.childNodes[0])

7 .Clone element

// jQuery
var clonedEl = $(&#39;.el&#39;).clone();

// Native
var clonedEl = document.querySelector(&#39;.el&#39;).cloneNode(true);

8.Remove element

Remove
// jQuery
$(&#39;.el&#39;).remove();

// Native
remove(&#39;.el&#39;);

function remove(el) {
 var toRemove = document.querySelector(el);

 toRemove.parentNode.removeChild(toRemove);
}

9.Get parent element

// jQuery
$(&#39;.el&#39;).parent();

// Native
document.querySelector(&#39;.el&#39;).parentNode;

10. Get the previous/next element (Prev/next element)

// jQuery
$(&#39;.el&#39;).prev();
$(&#39;.el&#39;).next();

// Native
document.querySelector(&#39;.el&#39;).previousElementSibling;
document.querySelector(&#39;.el&#39;).nextElementSibling;

11.XHR and AJAX

// jQuery
$.get(&#39;url&#39;, function (data) {

});
$.post(&#39;url&#39;, {data: data}, function (data) {

});

// Native

// get
var xhr = new XMLHttpRequest();

xhr.open(&#39;GET&#39;, url);
xhr.onreadystatechange = function (data) {

}
xhr.send();

// post
var xhr = new XMLHttpRequest()

xhr.open(&#39;POST&#39;, url);
xhr.onreadystatechange = function (data) {

}
xhr.send({data: data});

12 .Empty the child elements

//jQuery
$("#elementID").empty()

//Native
var element = document.getElementById("elementID")
while(element.firstChild) element.removeChild(element.firstChild);

13.Check whether there are child elements

//jQuery
if (!$("#elementID").is(":empty")){}

//Native
if (document.getElementById("elementID").hasChildNodes()){}

14.$(document).ready

When the DOM is loaded, the DOMContentLoaded event will be triggered, which is equivalent to jQuery's $(document).ready method.

document.addEventListener("DOMContentLoaded", function() {
  // ...
});

15. Data storage

jQuery objects can store data.

$("body").data("foo", 52);

HTML 5 has a dataset object, which has similar functions (not supported by IE 10), but it can only save strings.

element.dataset.user = JSON.stringify(user);
element.dataset.score = score;

16. Animation

jQuery’s animate method is used to generate animation effects.

$foo.animate(&#39;slow&#39;, { x: &#39;+=10px&#39; }

jQuery’s animation effects are largely based on DOM. But currently, CSS 3 animation is far more powerful than DOM, so you can write animation effects into CSS, and then display the animation by manipulating the classes of DOM elements.

foo.classList.add(&#39;animate&#39;)

If you need to use callback function for animation, CSS 3 also defines corresponding events.

el.addEventListener("webkitTransitionEnd", transitionEnded);
el.addEventListener("transitionend", transitionEnded);

The above is the detailed content of Detailed examples of 16 methods on how to replace JQuery with native js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn