轉載大牛的分析,這將是JSLite的方向。前人栽樹後人乘涼,jQuery爲咱們制定了一套接口標準,咱們繼續爲此努力。域名:JSLite.iophp
據統計,目前全世界57.3%的網站使用它。也就是說,10個網站裏面,有6個使用jQuery。若是隻考察使用工具庫的網站,這個比例就會上升到驚人的91.7%。css
雖然jQuery如此受歡迎,可是它臃腫的體積也讓人頭痛不已。jQuery 2.0的原始大小爲235KB,優化後爲81KB;若是是支持IE六、七、8的jQuery 1.8.3,原始大小爲261KB,優化後爲91KB。
這樣的體積,即便是寬帶環境,徹底加載也須要1秒或更長,更不要說移動設備了。這意味着,若是你使用了jQuery,用戶至少延遲1秒,才能看到網頁效果。考慮到本質上,jQuery只是一個操做DOM的工具,咱們不只要問:若是隻是爲了幾個網頁特效,是否有必要動用這麼大的庫?html
2006年,jQuery誕生的時候,主要用於消除不一樣瀏覽器的差別(主要是IE6),爲開發者提供一個簡潔的統一接口。相比當時,現在的狀況已經發生了很大的變化(Usage share of web browsers)。IE的市場份額不斷降低,以ECMAScript爲基礎的JavaScript標準語法,正獲得愈來愈普遍的支持。開發者直接使用JavScript標準語法,就能同時在各大瀏覽器運行,再也不須要經過jQuery獲取兼容性。node
下面就探討如何用JavaScript標準語法,取代jQuery的一些主要功能,作到jQuery-free。jquery
jQuery的核心是經過各類選擇器,選中DOM元素,能夠用querySelectorAll方法模擬這個功能。web
jsvar $ = document.querySelectorAll.bind(document);
這裏須要注意的是,querySelectorAll方法返回的是NodeList對象,它很像數組(有數字索引和length屬性),但不是數組,不能使用pop、push等數組特有方法。若是有須要,能夠考慮將Nodelist對象轉爲數組。ajax
jsmyList = Array.prototype.slice.call(myNodeList);
DOM自己就具備很豐富的操做方法,能夠取代jQuery提供的操做方法。
尾部追加DOM元素。數組
js // jQuery寫法 $(parent).append($(child)); // DOM寫法 parent.appendChild(child)
頭部插入DOM元素。瀏覽器
js // jQuery寫法 $(parent).prepend($(child)); // DOM寫法 parent.insertBefore(child, parent.childNodes[0])
刪除DOM元素。app
js // jQuery寫法 $(child).remove() // DOM寫法 child.parentNode.removeChild(child)
jQuery的on方法,徹底能夠用addEventListener模擬。
jsElement.prototype.on = Element.prototype.addEventListener;
爲了使用方便,能夠在NodeList對象上也部署這個方法。
jsNodeList.prototype.on = function (event, fn) { []['forEach'].call(this, function (el) { el.on(event, fn); }); return this; };
jQuery的trigger方法則須要單獨部署,相對複雜一些。
jsElement.prototype.trigger = function (type, data) { var event = document.createEvent('HTMLEvents'); event.initEvent(type, true, true); event.data = data || {}; event.eventName = type; event.target = this; this.dispatchEvent(event); return this; };
在NodeList對象上也部署這個方法。
jsNodeList.prototype.trigger = function (event) { []['forEach'].call(this, function (el) { el['trigger'](event); }); return this; };
目前的最佳實踐,是將JavaScript腳本文件都放在頁面底部加載。這樣的話,其實document.ready方法(jQuery簡寫爲$(function))已經沒必要要了,由於等到運行的時候,DOM對象已經生成了。
jQuery使用attr方法,讀寫網頁元素的屬性。
js$("#picture").attr("src", "http://url/to/image");
DOM元素容許直接讀取屬性值,寫法要簡潔許多。
js $("#picture").src = "http://url/to/image";
須要注意,input
元素的this.value
返回的是輸入框中的值,連接元素的this.href返回的是絕對URL。若是須要用到這兩個網頁元素的屬性準確值,能夠用 this.getAttribute('value')和this.getAttibute('href')
。
jQuery的addClass方法,用於爲DOM元素添加一個class。
js $('body').addClass('hasJS');
DOM元素自己有一個可讀寫的className屬性,能夠用來操做class。
js document.body.className = 'hasJS'; // or document.body.className += ' hasJS';
HTML 5還提供一個classList對象,功能更強大(IE 9不支持)。
js document.body.classList.add('hasJS'); document.body.classList.remove('hasJS'); document.body.classList.toggle('hasJS'); document.body.classList.contains('hasJS');
jQuery的css方法,用來設置網頁元素的樣式。
js $(node).css( "color", "red" );
DOM元素有一個style屬性,能夠直接操做。
js element.style.color = "red";; // or element.style.cssText += 'color:red';
jQuery對象能夠儲存數據。
js $("body").data("foo", 52);
HTML 5有一個dataset對象,也有相似的功能(IE 10不支持),不過只能保存字符串。
js element.dataset.user = JSON.stringify(user); element.dataset.score = score;
jQuery的Ajax方法,用於異步操做。
js $.ajax({ type: "POST", url: "some.php", data: { name: "John", location: "Boston" } }).done(function( msg ) { alert( "Data Saved: " + msg ); });
咱們能夠定義一個request函數,模擬Ajax方法。
jsfunction request(type, url, opts, callback) { var xhr = new XMLHttpRequest(); if (typeof opts === 'function') { callback = opts; opts = null; } xhr.open(type, url); var fd = new FormData(); if (type === 'POST' && opts) { for (var key in opts) { fd.append(key, JSON.stringify(opts[key])); } } xhr.onload = function () { callback(JSON.parse(xhr.response)); }; xhr.send(opts ? fd : null); }
而後,基於request函數,模擬jQuery的get和post方法。
jsvar get = request.bind(this, 'GET'); var post = request.bind(this, 'POST');
jQuery的animate方法,用於生成動畫效果。
js$foo.animate('slow', { x: '+=10px' });
jQuery的動畫效果,很大部分基於DOM。可是目前,CSS 3的動畫遠比DOM強大,因此能夠把動畫效果寫進CSS,而後經過操做DOM元素的class,來展現動畫。
js foo.classList.add('animate');
若是須要對動畫使用回調函數,CSS 3也定義了相應的事件。
js el.addEventListener("webkitTransitionEnd", transitionEnded); el.addEventListener("transitionend", transitionEnded);