Home > Web Front-end > JS Tutorial > body text

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

伊谢尔伦
Release: 2017-06-17 11:46:35
Original
1748 people have browsed it

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);
}
Copy after login

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

myList = Array.prototype.slice.call(myNodeList)
Copy after login

2.Create element

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

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

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);
});
Copy after login

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;);
Copy after login

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;);
Copy after login

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;));
Copy after login

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])
Copy after login

7 .Clone element

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

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

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);
}
Copy after login

9.Get parent element

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

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

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;
Copy after login

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});
Copy after login

12 .Empty the child elements

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

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

13.Check whether there are child elements

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

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

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() {
  // ...
});
Copy after login

15. Data storage

jQuery objects can store data.

$("body").data("foo", 52);
Copy after login

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;
Copy after login

16. Animation

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

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

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;)
Copy after login

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

el.addEventListener("webkitTransitionEnd", transitionEnded);
el.addEventListener("transitionend", transitionEnded);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!