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 + ' ');
}
};

When we deal with it ourselves , Even such a simple task is not used jquery It's going to get complicated when you're in the game . With the original js, We can use the following code snippet to add highlighted class :

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

Although it's a long time , But the solution still doesn't deal with a lot jquery In the list 1.2 Some of the things we've done in the past , Like the following :
1、 Deal with others appropriately window.load event
2、 stay DOM Start when the structure is ready .
3、 Use modern DOM Methods optimize element lookup and other tasks .

We can see that our jQuery-driven code is easier to write, simpler to read, and faster to execute than its plain JavaScript equivalent.

We can see clearly how we use query It's better than native code js The code is easier to write , It's easier to read , It runs faster .

The original js Code and jquery More related articles

  1. JQuery - Dynamic addition Html after , How to make CSS take effect ,JS Code available ?

    Today is developing JQuery Mobile In the process , You need to get data from the server , And then the Listview Control , The data is complete , Also dynamically added to Listview Control , But the data corresponds to CSS There's no effect , ...

  2. How to integrate Js The code is encapsulated in Jquery plug-in unit

    A lot of the same Jquery The code will be used on many pages , It's too much trouble to copy and paste every time , It's better to package it into a Jquery Plug in is convenient , As for the impact on the speed of the web page is not , I don't have a test . The code is as follows This is a custom flash print text Jquery , ...

  3. jQuery Validate Form validation plug-in ---- adopt name Property to associate fields to verify , Change the default message , Write the verification rule to js In the code

    One . Download dependency package Online disk download :https://yunpan.cn/cryvgGGAQ3DSW   Access password f224 Two . Add another plug-in jquery.validate.messages_cn.js. ...

  4. jQuery Picture scrolls seamlessly JS Code ul/li structure

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. jquery prohibit herf Jump , And execute the corresponding js Code

    <a class="oh_btn bg_3" href="javascript:void(0);" onclick="myfun(this)&q ...

  6. jquery Of js Code compatible with all browser solutions

    // Any of the following console.log() experiment   var winW=document.body.clientWidth||document.docuemntElement.clientWidth;// network ...

  7. ****timeago.js plug-in unit :jquery A few minutes ago 、 A few hours ago 、 A few days ago, the code example of time difference display effect

    front end Time personalization plug-in unit jquery.timeago.js key word : Time format   just  N Minutes ago  N Hours before  N Days ago,  N Month ago,  N Years ago MM-dd hh:mm   perhaps  yyyy-MM-dd front end : & ...

  8. [JQuery] use InsertAfter Realize the effect of picture lantern display 2——js Code refactoring

    Write it at the front I wrote an article earlier <[JQuery] use InsertAfter Realize the effect of picture lantern display >, Ever since I wrote that, it can be regarded as using the object-oriented writing method , The code is ugly , Ever since I wrote that code , It's just a piece of my heart ...

  9. The most complete in history CSS hack At a glance jQuery Image carousel code separation JQuery In the animation C# in Trim()、TrimStart()、TrimEnd() Usage of marquee Details of the use of the label js Mouse events js Add mask layer There are two solutions to garbled code when transferring value through address bar on the page ref and out The difference is c# in summary

    The most complete in history CSS hack At a glance 2013 year 09 month 28 Japan 15:57:08 Read the number :175473 For many years , Although not often hack, However, we often encounter inconsistencies among browsers . Based on this , In some cases, I ...

Random recommendation

  1. [ turn ] IIS Profile's XML Incorrect format applicationHost.config collapse Recovery solutions

    IIS Profile's XML Incorrect format applicationHost.config collapse Recovery solutions Source file :http://www.cnblogs.com/yuejin/p/3385584.html   ...

  2. Single page implementation hash

    It's time to learn angularJs after , I don't know if this single page application is available angularJs How to achieve in the case of . So this thinking goes hand in hand with the data , Finally know the basic principle of this implementation . First angularJs The implementation of hash The change of value ...

  3. Natural language 16_Chunking with NLTK

    Chunking with NLTK Yes chunk The classified data structure can be output graphically , It is used to analyze the main structure of English sentences # -*- coding: utf-8 -*-"""Created ...

  4. K-meams Text clustering algorithm C++ Realization

    FROM:http://www.cnblogs.com/finallyliuyu/archive/2010/09/03/1817348.html The header file : #ifndef _Preprocess_H ...

  5. Use maven After management , I still can't find what I need jar package

    Use maven After management , Still wrong , Can't find , For example, the following error java.lang.ClassNotFoundException: org.springframework.web.context.ContextL ...

  6. oracle Database administrator

    One . The database administrator each oracle The database should have at least one database administrator (dba), For a small database , One dba That's enough , But for a large database, multiple dba Sharing different management responsibilities . So the main function of a database administrator is ...

  7. Wang Zhitai 201771010131《 Object oriented programming (java)》 Week 12 learning summary

    The first part : Theoretical knowledge learning part The first 10 Chapter Graphic programming 10.1 AWT And Swing brief introduction 1. The user interface (User Interface) The concept of : Users and computer systems ( All kinds of programs ) Interface of interaction 2. Graphical user interface (Gr ...

  8. Docker inspect - format Format output - Operation and Maintenance Notes

    Docker --format The parameter provides a method based on Go Templates Log format output auxiliary function of , And some built-in enhancement functions are provided . What is a template ? This is a familiar picture MVC frame (Model View Controller ...

  9. ElasticStack Series 12 &amp; Search results research

    problem Use ElasticSearch Do a search when , For example, user input --> lemon , The results of the search --> lemon squash , Lemon toothpaste waiting in front , The fruit you really want lemon rearwards . It has been added to Chinese word segmentation ...

  10. AJAX,JSON,GSON

    AJAX Use data JSON The format is sent to the back end Servlet Or other language parsing . Yes JSON Content to use GSON The external expansion package is decomposed , And use ( For example, query whether the user name has been registered ), Finally using Map Collection sets a new return status code , And use ...