- Learning jQuery(Fourth Edition)
- Jonathan Chaffer Karl Swedberg
- 162字
- 2021-08-13 17:18:44
Plain JavaScript versus jQuery
Even a task as simple as this can be complicated without jQuery at our disposal. In plain JavaScript, we could add the highlight
class in this way:
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 + ' '); } };
Listing 1.3
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:
- Properly respecting other
window.onload
event handlers - Acting as soon as the DOM is ready
- Optimizing element retrieval and other tasks with modern DOM methods
We can see that our jQuery-driven code is easier to write, simpler to read, and faster to execute than its plain JavaScript equivalent.
推薦閱讀
- Mastering Concurrency Programming with Java 8
- Spring 5.0 Microservices(Second Edition)
- 玩轉Scratch少兒趣味編程
- 深入理解Django:框架內幕與實現原理
- 基于免疫進化的算法及應用研究
- Oracle Database 12c Security Cookbook
- MySQL數據庫管理與開發(慕課版)
- Learning Three.js:The JavaScript 3D Library for WebGL
- 用戶體驗增長:數字化·智能化·綠色化
- Instant Lucene.NET
- C專家編程
- Mastering Python Design Patterns
- Android嵌入式系統程序開發:基于Cortex-A8(第2版)
- Mastering AWS Security
- H5+移動營銷設計寶典