原始的js代碼和jquery對比體會
更新時間:2013年09月10日 16:08:51 作者:
在我們自己處理的時候,甚至是這么簡單的任務(wù)在不使用jquery的時候都會變得復(fù)雜,通過下面我們可以清晰的看到使用query代碼比原生js代碼寫起來更容易
Even a task as simple as this can be complicated without jQuery at our disposal. In plain JavaScript, we could add the highlightedclass as shown in the following code snippet:
window.onload = function() {
var divs = document.getElementsByTagName('div');
for (var i = 0; i < divs.length; i++) {
if (hasClass(divs[i], 'poem-stanza') && !hasClass(divs[i], 'highlight')) {
divs[i].className += ' highlight';
}
}
function hasClass( elem, cls ) {
var reClass = new RegExp(' ' + cls + ' ');
return reClass.test(' ' + elem.className + ' ');
}
};
在我們自己處理的時候,甚至是這么簡單的任務(wù)在不使用jquery的時候都會變得復(fù)雜。用原始的js,我們可以使用下面的代碼片段添加highlighted類:
Despite its length, this solution does not handle many of the situations that jQuery takes care of for us in Listing 1.2, such as the following:
• Properly respecting other window.onloadevent handlers
• Acting as soon as the DOM is ready
• Optimizing element retrieval and other tasks with modern DOM methods
盡管很長,但是這個解決方案依然沒有處理很多jquery在列表1.2中為我們做到的一些事情,比如下面的這些:
1、合適的處理其他的window.load事件
2、在DOM結(jié)構(gòu)準備好的時候開始行動。
3、使用現(xiàn)代的DOM方法優(yōu)化元素查找和其他任務(wù)。
We can see that our jQuery-driven code is easier to write, simpler to read, and faster to execute than its plain JavaScript equivalent.
我們可以清晰的看到我們的使用query的代碼比原生js代碼寫起來更容易,讀起來更簡單,運行起來更快。
復(fù)制代碼 代碼如下:
window.onload = function() {
var divs = document.getElementsByTagName('div');
for (var i = 0; i < divs.length; i++) {
if (hasClass(divs[i], 'poem-stanza') && !hasClass(divs[i], 'highlight')) {
divs[i].className += ' highlight';
}
}
function hasClass( elem, cls ) {
var reClass = new RegExp(' ' + cls + ' ');
return reClass.test(' ' + elem.className + ' ');
}
};
在我們自己處理的時候,甚至是這么簡單的任務(wù)在不使用jquery的時候都會變得復(fù)雜。用原始的js,我們可以使用下面的代碼片段添加highlighted類:
Despite its length, this solution does not handle many of the situations that jQuery takes care of for us in Listing 1.2, such as the following:
• Properly respecting other window.onloadevent handlers
• Acting as soon as the DOM is ready
• Optimizing element retrieval and other tasks with modern DOM methods
盡管很長,但是這個解決方案依然沒有處理很多jquery在列表1.2中為我們做到的一些事情,比如下面的這些:
1、合適的處理其他的window.load事件
2、在DOM結(jié)構(gòu)準備好的時候開始行動。
3、使用現(xiàn)代的DOM方法優(yōu)化元素查找和其他任務(wù)。
We can see that our jQuery-driven code is easier to write, simpler to read, and faster to execute than its plain JavaScript equivalent.
我們可以清晰的看到我們的使用query的代碼比原生js代碼寫起來更容易,讀起來更簡單,運行起來更快。
相關(guān)文章
JavaScript顯式數(shù)據(jù)類型轉(zhuǎn)換詳解
這篇文章主要介紹了JavaScript顯式數(shù)據(jù)類型轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03深入理解JavaScript系列(30):設(shè)計模式之外觀模式詳解
這篇文章主要介紹了深入理解JavaScript系列(30):設(shè)計模式之外觀模式詳解,外觀模式(Facade)為子系統(tǒng)中的一組接口提供了一個一致的界面,此模塊定義了一個高層接口,這個接口值得這一子系統(tǒng)更加容易使用,需要的朋友可以參考下2015-03-03onkeyup,onkeydown和onkeypress的區(qū)別介紹
三者在事件的響應(yīng)上還有一點不同,就是onkeydown 、onkeypress事件響應(yīng)的時候輸入的字符并沒有被系統(tǒng)接受,而響應(yīng)onkeyup的時候,輸入流已經(jīng)被系統(tǒng)接受2013-10-10JavaScript中require和import的區(qū)別詳解
本文詳細講解了JS中require和import的區(qū)別,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06