[教学] 越来越像 jQuery 的原生 JavaScript

楼主: jmlntw (吉米林)   2017-08-07 21:55:15
https://dom.spec.whatwg.org/#interface-childnode
DOM manipulation convenience methods 是 WHATWG 的 Living Standard,
提供更接近 jQuery 用法的 DOM API。
【移除】
jQuery: $('.someClass').remove();
原生: document.querySelector('.someClass').remove();
【Prepend】
jQuery: $('.someClass').prepend('hello world');
原生: document.querySelector('.someClass').prepend('hello world');
【Append】
jQuery: $('.someClass').append('hello world');
原生: document.querySelector('.someClass').append('hello world');
【Before】
jQuery: $('.someClass').before('hello world');
原生: document.querySelector('.someClass').before('hello world');
【After】
jQuery: $('.someClass').after('hello world');
原生: document.querySelector('.someClass').after('hello world');
【取代】
jQuery: $('.someClass').replaceWith(element);
原生: document.querySelector('.someClass').replaceWith(element);
【For-Loop 所有相符的元素】
jQuery: $('.someClass').each(function () { ... });
原生: document.querySelectorAll('.someClass').forEach(element => { ... })
(NodeList 可以直接 forEach() 了。)
【浏览器支援程度】
除了 IE 和 Edge 外其他主流浏览器的最新版本都 OK。
作者: LPH66 (-6.2598534e+18f)   2017-08-07 22:52:00
说到 each, 我对 jQuery 的 each 跟原生 JS 的 forEachcallback 的参数顺序不一样感觉各种囧...jQuery 的 each 是 (index, element)=>...原生 JS 的 forEach 是 (element, index) => ...
作者: visa9527 (高级伴读士官长)   2017-08-09 14:31:00
element 先比较符合传统因为 Array.map() 也是先 value 才 indexIE不支援就算了,Edge 不支援比较麻烦
作者: shadowjohn (转角遇到爱)   2017-08-14 09:22:00
继续努力,让不能用的都可以用吧
作者: hijkxyzuw (i,j,k) ×(x,y,z)   2017-09-08 00:42:00
append 和 appendChild 不一样也蛮冏的
作者: shadowjohn (转角遇到爱)   2017-09-13 11:10:00
请持续努力直到作出下一个jQuerylite

Links booklink

Contact Us: admin [ a t ] ucptt.com