vanilla-js-dom 的使用教程 :)
https://github.com/Haeresis/vanilla-js-dom/blob/master/README.md
github地址:
https://github.com/Haeresis/vanilla-js-dom
Vanilla JS
其实你什么框架都不需要!
Overview
Vanilla JS is a fast, lightweight, cross-platform tool for building incredible, powerful JavaScript applications.
Use in Development
Just put the following code:
1 | <!-- <script src="path/to/vanilla.js"></script> --> |
Use in Production
The much faster method:
1 |
Speed Comparison
When Vanilla JS perform 100 operations, others do:
Retrieve DOM element by ID
| Code | 100 ops Vanilla JS | |
|---|---|---|
| Vanilla JS | document.getElementById(‘vanilla’); | 100 |
| Dojo | dojo.byId(‘dojo’); | 92 |
| Prototype JS | $(‘prototype’); | 57 |
| jQuery | $(‘#jquery’); | 42 |
| MooTools | document.id(‘mootools’); | 24 |
Retrieve 10 DOM elements by tag name
| Code | 100 ops Vanilla JS | |
|---|---|---|
| Vanilla JS | document.getElementsByTagName(‘div’); | 100 |
| Prototype JS | Prototype.Selector.select(‘div’, document); | 25 |
| jQuery | $(‘div’); | 21 |
| Dojo | dojo.query(‘div’); | 3 |
| MooTools | Slick.search(document, ‘div’, new Elements); | 2 |
Vanilla JS vs jQuery
Retrieve 10 DOM elements by class name
| Code | 100 ops Vanilla JS | |
|---|---|---|
| Vanilla JS | document.getElementsByClassName(‘vanilla’); | 100 |
| jQuery | $(‘.jquery’); | 25 |
Retrieve DOM element with <#id> .inner span selector
| Code | 100 ops Vanilla JS | |
|---|---|---|
| Vanilla JS | document.querySelector(‘#vanilla .inner span’); | 100 |
| jQuery | $(‘#jquery .inner span’); | 17 |
Retrieve 10 DOM elements with <.className> .inner span selector
| Code | 100 ops Vanilla JS | |
|---|---|---|
| Vanilla JS | document.querySelectorAll(‘.vanilla .inner span’); | 100 |
| jQuery | $(‘.jquery .inner span’); | 51 |
Vanilla JS Selector Performances
All tests are based on <section id="vanilla" class="vanilla"><article class="inner"><div class="target" id="target"></div></article></section> HTML.
Select node <div class="target" id="target"></div> |
100 ops Vanilla JS |
|---|---|
| document.getElementsByTagName(‘div’); | 100 |
| document.getElementById(‘target’); | 99 |
| document.getElementsByClassName(‘target’); | 96 |
| document.querySelector(‘.vanilla .inner div’); | 68 |
| document.querySelectorAll(‘.vanilla .inner div’); | 35 |
From jQuery to Vanilla JS
Legend
Understand each type of DOM Object:
1 | <div class="example"> |
querySelector('.example')return aHTMLElement.querySelector('.example').childrenreturn aHTMLCollection, each collection’s item is aHTMLElement, two[span, span]here.querySelector('.example').childNodesreturn aNodeList, each collection’s item is aNode, seven[text, span, text, comment, text, span, text]here.querySelector('.example').childNodes[0]return aText(Node) oftypeNode3, as a text. (...nodeList[3]istypeNode8 as aComment(Nodetoo)).
.Node #Selector
#id
From jQuery
1 | var htmlElement = $('#id')[0]; |
to Vanilla JS
1 | var htmlElement = document.getElementById('id'); |
.classname #id tagname
From jQuery
1 | var htmlElement = $('#id .classname tagname')[0]; |
to Vanilla JS
1 | document.querySelector('#id .classname tagname'); |
[.classname #id tagname]
From jQuery
1 | $('#id .classname tagname').each(function (i, htmlElement) { |
to Vanilla JS
1 | var nodeList = document.querySelectorAll('#id .classname tagname'); // Not Live (Snapshot) |
[.classname]
From jQuery
1 | $('.classname').each(function (i, htmlElement) { |
to Vanilla JS
1 | var htmlCollection = document.getElementsByClassName('classname'); // Live |
[name]
From jQuery
1 | $('[name="name"]').each(function (i, htmlElement) { |
to Vanilla JS
1 | var nodeList = document.getElementsByName('name'); // Live |
[tagname]
From jQuery
1 | $('tagname').each(function (i, htmlElement) { |
to Vanilla JS
1 | var htmlCollection = document.getElementsByTagName('tagname'); // Live |
Reverted Loop
From jQuery
1 | $($('.className').get().reverse()).each(function (i, htmlElement) { |
to Vanilla JS
1 | var htmlCollection = document.getElementsByClassName('className'), // Live |
AJAX
GET
From jQuery
1 | $.ajax({ |
to Vanilla JS
1 | fetch(<url>, { |
JSON
From jQuery
1 | function checkStatus(response) { |
to Vanilla JS
1 | function getJSON(url, next) { |
POST
From jQuery
1 | $.ajax({ |
to Vanilla JS
1 | fetch(<url>, { |
Request / Response
From jQuery
1 | function checkStatus(response) { |
to Vanilla JS
1 | function request(url, next) { |
ATTRIBUTS
Add Class
From jQuery
1 | $(<htmlElement>).addClass(<className>); |
to Vanilla JS
1 | <htmlElement>.classList.add(<className>); |
Get Attribute
From jQuery
1 | $(<htmlElement>).attr(<attributeName>); |
to Vanilla JS
1 | <htmlElement>.getAttribute(<attributeName>); |
Get Data
From jQuery
1 | $(<htmlElement>).data(<dataName>); |
to Vanilla JS
1 | <htmlElement>.getAttribute('data-' + <dataName>); |
Get Value
From jQuery
1 | $(<htmlElement>).val(); |
to Vanilla JS
1 | <htmlElement>.value; |
Has Class
From jQuery
1 | $(<htmlElement>).hasClass(<className>); |
to Vanilla JS
1 | <htmlElement>.classList.contains(<className>); |
Remove Attribute
From jQuery
1 | $(<htmlElement>).removeAttr(<attributeName>); |
to Vanilla JS
1 | <htmlElement>.removeAttribute(<attributeName>); |
Remove Class
From jQuery
1 | $(<htmlElement>).removeClass(<className>); |
to Vanilla JS
1 | <htmlElement>.classList.remove(<className>); |
Remove Data
From jQuery
1 | $(<htmlElement>).removeData(<dataName>); |
to Vanilla JS
1 | <htmlElement>.removeAttribute('data-' + <dataName>); |
Set Attribute
From jQuery
1 | $(<htmlElement>).attr(<attributeName>, <value>); |
to Vanilla JS
1 | <htmlElement>.setAttribute(<attributeName>, <value>); |
Set Data
From jQuery
1 | $(<htmlElement>).data(<dataName>, <value>); |
to Vanilla JS
1 | <htmlElement>.setAttribute('data-' + <dataName>, <value>); |
Set Value
From jQuery
1 | $(<htmlElement>).val(<value>); |
to Vanilla JS
1 | <htmlElement>.value = <value>; |
Toggle Class
From jQuery
1 | $(<htmlElement>).toggleClass(<className>); |
to Vanilla JS
1 | <htmlElement>.classList.toggle(<className>); |
EFFECTS
Animation
From jQuery
1 | function fadeIn($htmlElement, speed, next) { |
to Vanilla JS
1 | function fadeIn(htmlElement, speed, next) { |
Hide
From jQuery
1 | $(<htmlElement>).hide(); |
to Vanilla JS
1 | <htmlElement>.style.display = 'none'; |
Show
From jQuery
1 | $(<htmlElement>).show(); |
to Vanilla JS
1 | <htmlElement>.style.display = ''; |
EVENTS
Hover
From jQuery
1 | $(<htmlElement>).hover(<eventHandlerMouseIn>, <eventHandlerMouseOut>); |
to Vanilla JS
1 | <htmlElement>.addEventListener('mouseenter', <eventHandlerMouseIn>); |
Load
From jQuery
1 | $(<htmlElement>).load(function () { |
to Vanilla JS
1 | <htmlElement>.addEventListener('load', function () { |
Off
From jQuery
1 | $(<htmlElement>).off(<eventName>, <eventHandler>); |
to Vanilla JS
1 | <htmlElement>.removeEventListener(<eventName>, <eventHandler>); |
On
From jQuery
1 | $(<htmlElement>).on(<eventName>, <eventHandler>); |
to Vanilla JS
1 | <htmlElement>.addEventListener(<eventName>, <eventHandler>); |
One
From jQuery
1 | $(<htmlElement>).one(<eventName>, <eventHandler>); |
to Vanilla JS
1 | <htmlElement>.addEventListener(<eventName>,<eventHandler>,{once: true}); |
Ready
From jQuery
1 | $(document).ready(function () { |
to Vanilla JS
1 | document.addEventListener('DOMContentLoaded', function () { |
Trigger
From jQuery
1 | var event = jQuery.Event('click'); |
to Vanilla JS
1 | var event = new Event('click'); |
FILTERS
Filter
From jQuery
filterCondition
1 | $(<selector>).filter(function (i, htmlElement) { |
to Vanilla JS
1 | var nodeList = document.querySelectorAll(<selector>); |
First
From jQuery
1 | $(<selector>).first(); |
to Vanilla JS
1 | <htmlCollection>.item(0); |
Has
From jQuery
1 | $(<selector>).has(<matchesChildSelector>); |
to Vanilla JS
1 | var nodeList = document.querySelectorAll(<selector>); |
Is
From jQuery
1 | $(<selector>).is(<matchesSelector>); |
to Vanilla JS
1 | var nodeList = document.querySelectorAll(<selector>); |
Item
From jQuery
1 | $(<selector>).eq(<index>); |
to Vanilla JS
1 | <htmlCollection>.item(<index>); |
Last
From jQuery
1 | $(<selector>).last(); |
to Vanilla JS
1 | <htmlCollection>.item(<htmlCollection>.length - 1); |
Not
From jQuery
1 | $(<selector>).not(<matchesSelector>); |
to Vanilla JS
1 | var nodeList = document.querySelectorAll(<selector>); |
Slice
From jQuery
1 | $(<selector>).slice(<startIndex>, <endIndex>); |
to Vanilla JS
1 | var nodeList = document.querySelectorAll(<selector>); |
MANIPULATION
Append
From jQuery
1 | $(<htmlElement>).append($(<appendHtmlElement>)); |
to Vanilla JS
1 | <htmlElement>.appendChild(<appendHtmlElement>); |
Clone
From jQuery
1 | $(<htmlElement>).clone(); |
to Vanilla JS
1 | <htmlElement>.cloneNode(true); |
Compare
From jQuery
1 | var $a = $(<selectorToFirstHtmlElement>).find(<selectorToSecondHtmlElement>); |
to Vanilla JS
1 | var temp = document.getElementsByTagName(<selectorToFirstHtmlElement>)[0], |
Contains
From jQuery
1 | $.contains($(<htmlElement>), $(<childHtmlElement>)); |
to Vanilla JS
1 | (<htmlElement> !== <childHtmlElement>) && <htmlElement>.contains(<childHtmlElement>); |
Create
From jQuery
1 | $('<' + <tagString> + '>'); |
to Vanilla JS
1 | document.createElement(<tagString>); |
Empty
From jQuery
1 | $(<htmlElement>).empty(); |
to Vanilla JS
1 | <htmlElement>.innerHTML = ''; |
Get HTML
From jQuery
1 | $(<htmlElement>).html(); |
to Vanilla JS
1 | <htmlElement>.innerHTML; |
Get Node HTML
From jQuery
1 | $('<div>').append($(<htmlElement>).clone()).html(); |
to Vanilla JS
1 | <htmlElement>.outerHTML; |
Get Text
From jQuery
1 | $(<htmlElement>).text(); |
to Vanilla JS
1 | <htmlElement>.textContent; |
Index From Parent
From jQuery
1 | $(<htmlElement>).index(); |
to Vanilla JS
1 | [].slice.call(<htmlElement>.parentNode.children).indexOf(<htmlElement>); |
Insert After
From jQuery
1 | $(<htmlElement>).after($(<afterHtmlElement>)); |
to Vanilla JS
1 | <htmlElement>.parentNode.insertBefore(<afterHtmlElement>, <htmlElement>.nextSibling); |
Insert Before
From jQuery
1 | $(<htmlElement>).before($(<beforeHtmlElement>)); |
to Vanilla JS
1 | <htmlElement>.parentNode.insertBefore(<beforeHtmlElement>, <htmlElement>); |
Prepend
From jQuery
1 | $(<htmlElement>).prepend($(<prependHtmlElement>)); |
to Vanilla JS
1 | <htmlElement>.insertBefore(<prependHtmlElement>, <htmlElement>.firstChild); |
Remove
From jQuery
1 | $(<htmlElement>).remove(); |
to Vanilla JS
1 | <htmlElement>.parentNode.removeChild(<htmlElement>); |
Remove Children
From jQuery
1 | $(<htmlElement>).empty(); |
to Vanilla JS
1 | while (<htmlElement>.firstChild) { |
Replace
From jQuery
1 | $(<htmlElement>).replaceWith($(<newHtmlElement>)); |
to Vanilla JS
1 | <htmlElement>.parentNode.replaceChild(<newHtmlElement>, <htmlElement>); |
Set HTML
From jQuery
1 | $(<htmlElement>).html(<htmlString>); |
to Vanilla JS
1 | <htmlElement>.innerHTML = <htmlString>; |
Set Node HTML
From jQuery
1 | $(<htmlElement>).replaceWith(<htmlString>); |
to Vanilla JS
1 | <htmlElement>.outerHTML = <htmlString>; |
Set Text
From jQuery
1 | $(<htmlElement>).text(<string>); |
to Vanilla JS
1 | <htmlElement>.textContent = <string>; |
Unwrap
From jQuery
1 | $(<htmlElement>).unwrap(); |
to Vanilla JS
1 | while (<htmlElement>.firstChild) { |
Wrap
From jQuery
1 | $(<htmlElement>).wrap($(<wrapHtmlElement>)); |
to Vanilla JS
1 | <htmlElement>.parentNode.insertBefore(<wrapHtmlElement>, <htmlElement>); |
TRAVERSING
All Next
From jQuery
1 | $(<htmlElement>).nextAll(); |
to Vanilla JS
1 | var nextAll = false; |
All Parents
From jQuery
1 | var parents = $(<htmlElement>).parents(); |
to Vanilla JS
1 | var htmlElement = <htmlElement>, |
All Previous
From jQuery
1 | $(<htmlElement>).prevAll(); |
to Vanilla JS
1 | var prevAll = true; |
Children
From jQuery
1 | $(<htmlElement>).children(); |
to Vanilla JS
1 | <htmlElement>.children; |
Closest Parent
From jQuery
1 | $(<htmlElement>).closest(<parentSelector>); |
to Vanilla JS
1 | <htmlElement>.closest(<parentSelector>); |
Find Children
From jQuery
1 | $(<htmlElement>).find(<childrenSelector>); |
to Vanilla JS
1 | <htmlElement>.querySelectorAll(<childrenSelector>); |
First Child
From jQuery
1 | $(<htmlElement>).children().first(); |
to Vanilla JS
1 | <htmlElement>.firstChild(); |
Last Child
From jQuery
1 | $(<htmlElement>).children().last(); |
to Vanilla JS
1 | <htmlElement>.lastChild(); |
Matches Selector
From jQuery
1 | $(<htmlElement>).is(<selector>); |
to Vanilla JS
1 | <htmlElement>.matches(<selector>); |
Next
From jQuery
1 | $(<htmlElement>).next(); |
to Vanilla JS
1 | <htmlElement>.nextElementSibling; // HTMLElement |
Next Until
From jQuery
1 | $(<htmlElement>).nextUntil(<nextSelector>); |
to Vanilla JS
1 | var htmlElement = <htmlElement>, |
Parent
From jQuery
1 | $(<htmlElement>).parent(); |
to Vanilla JS
1 | <htmlElement>.parentNode; |
Parents
From jQuery
1 | var parents = $(<htmlElement>).parents(<parentSelector>); |
to Vanilla JS
1 | var htmlElement = <htmlElement>, |
Parents Until
From jQuery
1 | var parents = $(<htmlElement>).parentsUntil(<parentSelector>); |
to Vanilla JS
1 | var htmlElement = <htmlElement>, |
Previous
From jQuery
1 | $(<htmlElement>).prev(); |
to Vanilla JS
1 | <htmlElement>.previousElementSibling; // HTMLElement |
Previous Until
From jQuery
1 | $(<htmlElement>).prevUntil(<previousSelector>); |
to Vanilla JS
1 | var htmlElement = <htmlElement>, |
Siblings
From jQuery
1 | $(<htmlElement>).siblings(); |
to Vanilla JS
1 | [].filter.call(<htmlElement>.parentNode.children, function (htmlElement) { |
STYLES
Get Style
From jQuery
1 | $(<htmlElement>).css(<property>); |
to Vanilla JS
1 | getComputedStyle(<htmlElement>)[<property>]; |
Get Scroll Left
From jQuery
1 | $(<htmlElement>).scrollLeft(); |
to Vanilla JS
1 | <htmlElement>.scrollLeft; |
Get Scroll Top
From jQuery
1 | $(<htmlElement>).scrollTop(); |
to Vanilla JS
1 | <htmlElement>.scrollTop; |
Inner Height
From jQuery
1 | $(<htmlElement>).innerHeight(); |
to Vanilla JS
1 | <htmlElement>.clientHeight |
Inner Width
From jQuery
1 | $(<htmlElement>).innerWidth(); |
to Vanilla JS
1 | <htmlElement>.clientWidth |
Offset from Document
From jQuery
1 | $(<htmlElement>).offset(); |
to Vanilla JS
1 | var rect = <htmlElement>.getBoundingClientRect() |
Offset from Parent
From jQuery
1 | $(<htmlElement>).position(); |
to Vanilla JS
1 | { |
Offset from Viewport
From jQuery
1 | $(<htmlElement>).offset(); |
to Vanilla JS
1 | var rect = <htmlElement>.getBoundingClientRect() |
Outer Height
From jQuery
1 | $(<htmlElement>).outerHeight(); |
to Vanilla JS
1 | <htmlElement>.offsetHeight |
Outer Width
From jQuery
1 | $(<htmlElement>).outerWidth(); |
to Vanilla JS
1 | <htmlElement>.offsetWidth |
Parent Not Static
From jQuery
1 | $(<htmlElement>).offsetParent(); |
to Vanilla JS
1 | (<htmlElement>.offsetParent || <htmlElement>) |
Set Style
From jQuery
1 | $(<htmlElement>).css(<property>, <value>); |
to Vanilla JS
1 | <htmlElement>.style.<property> = <value>; |
Set Scroll Left
From jQuery
1 | $(<htmlElement>).scrollLeft(<distance>); |
to Vanilla JS
1 | <htmlElement>.scrollLeft = <distance>; |
Set Scroll Top
From jQuery
1 | $(<htmlElement>).scrollTop(<distance>); |
to Vanilla JS
1 | <htmlElement>.scrollTop = <distance>; |
UTILS
Array Each
From jQuery
1 | $.each(<array>, function (i, item) { |
to Vanilla JS
1 | <array>.forEach(function (item, i) { |
Change Futur Context
From jQuery
1 | $.proxy(<fn>, <context>); |
to Vanilla JS
1 | <fn>.bind(<context>); |
Extend
From jQuery
1 | <object> = $.extend(<extendingObject>, <object>); |
to Vanilla JS
1 | <object> = Object.assign(<object>, <extendingObject>); |
Index Of
From jQuery
1 | $.inArray(<item>, <array>); |
to Vanilla JS
1 | <array>.indexOf(<item>); |
Is Array
From jQuery
1 | $.isArray(<array>); |
to Vanilla JS
1 | Array.isArray(<array>); |
Map
From jQuery
1 | $.map(<array>, function (item, i) { |
to Vanilla JS
1 | <array>.map(function (item, i) { |
Now
From jQuery
1 | $.now(); |
to Vanilla JS
1 | Date.now(); |
Parse HTML
From jQuery
1 | $.parseHTML(<htmlString>); |
to Vanilla JS
1 | function parseHTML(htmlString) { |
Parse JSON
From jQuery
1 | $.parseJSON(<jsonString>); |
to Vanilla JS
1 | JSON.parse(<jsonString>); |
Parse XML
From jQuery
1 | $.parseXML(<xmlString>); |
to Vanilla JS
1 | function parseXML(xmlString) { |
Serialize Array
From jQuery
1 | $.serializeArray(<form>); |
to Vanilla JS
1 | function serializeArray(form) { |
Serialize String
From jQuery
1 | $.serialize(<form>); |
to Vanilla JS
1 | function serialize(form) { |
Trim
From jQuery
1 | $.trim(<string>); |
to Vanilla JS
1 | <string>.trim(); |