Our custom code will go in the second, currently empty, JavaScript file which we wrote ded from the HTML using <script src = "01.js"> </script>. for this example, we only need three lines of code, as follows: $ (document ). ready (function () {$ ('div. poem-stanza '). addClass ('highlight') ;}); our common code will be written in the following JavaScript files that are still empty, in html, we use <script src = "01.js"> </script> to include the file. For example, we only need the following three lines of code: $ (document ). ready (function () {$ ('div. poem-stanza '). addClass ('highlight');}); Finding the poem text Search element The fundamental operation in jQuery is selecting a part of the document. this is done with the $ () function. typically, it takes a string as a parameter, which can contain any CSS selector expression. in this case, we wish to find all of the <div> elements in the document that have th E poem-stanzaclass applied to them, so the selector is very simple. however, we will cover much more sophisticated options through the course of the book. we will step through several ways of locating parts of a document in Chapter 2, Selecting Elements. the most fundamental operation of jquery is to find a part of the document, which is done using the $ () method. Generally, it uses a string as a parameter, which can contain a css selection expression. In this scenario, we want to find all the div elements with the poem-stanza class in the document, so the selector will be very simple. However, we hope that through this book's tutorial, we can learn more complex operations. In Chapter 2 "Search elements", we will gradually use multiple methods to locate the Document section. When called, the $ () function returns a new jQuery object instance, which is the basic building block we will be working with from now on. this object encapsulates zero or more DOM elements, and allows us to interact with them in each different ways. in this case, we wish to modify the appearance of these parts of the page, and we will accomplish this by changing the classes applied to the poem text. When $ () is called, a new jquery object instance will be returned. From now on, this will be the basic building content we will process. This object encapsulates zero or more dom elements and allows us to influence them in multiple ways. In this case, we want to modify the representation of these parts on the web page. We will modify the method of the class attached to the web page to achieve this goal. Injecting the new class injects a new class. addClass () method, like most jQuery methods, is named self-descriptively; it applies a CSS class to the part of the page that we have selected. its only parameter is the name of the class to add. this method, and its counterpart ,. removeClass (), will allow us to easily observe jQuery in action as we have e the different selector expressions available to us. for now, Our example simply adds the highlightclass, which our stylesheet has defined as italicized text with a gray background and a border .. like most jquery methods, the addClass () method uses its own function description to name itself. He will add a new class for the part of the selected webpage. His unique parameter is the name of the class to be added. As long as we find different selectors that are effective to us, this method and its corresponding method. removeClass () make it easy for us to use jquery. Now, the example simply adds a highlight class. We define it as italic text with a gray background and border in the css file. Note that no iteration is necessary to add the class to all the poem stanzas. as we discussed, jQuery uses implicit iterationwithin methods such. addClass (), so a single function call is all it takes to alter all of the selected parts of the document. note that we do not need to iterate to add classes for all the searched elements. As we have discussed, jquery is. the addClass () method uses implicit iteration. Therefore, a separate method is called to modify all the content in the selected document. Executing the code Execution code Taken together, $ () and. addClass () are enough for us to accomplish our goal of changing the appearance of the poem text. however, if this line of code is inserted alone in the document header, it will have no effect. javaScript code is generally run as soon as it is encountered in the browser, and at the time the header is being processed, no HTML is yet present to style. we n Eed to delay the execution of the code until after the DOM is available for our use. Using $ () and. addClass () is sufficient for us to accomplish our goal of modifying document performance. However, if this line of code is inserted only to the header of the document, it will not work. JavaScript code will be executed when it encounters a browser. At this time, the header file is being processed, but no html document is displayed. We need to delay the execution of the Code until the dom structure is ready for use. With the $ (document ). ready () construct, jQuery allows us to schedule function callfor firing once the DOM is loaded-without necessarily waiting for images to fully render. while this event scheduling is possible without the aid of jQuery, $ (document ). ready () provides an especially elegant cross-browser solution that: use $ (document ). in the ready () structure, jquery allows us to execute functions after the DOM structure is loaded, without waiting until all the images are completely rendered. Although the event arrangement can be achieved without the help of jquery, $ (document ). ready () provides an elegant cross-browser solution: • Uses the browser's native DOM ready implementations when available and adds a window. onloadevent handler as a safety net • Allows for multiple callto $ (document ). ready () and executes them in the order in which they are called • Executes functions passed to $ (document ). ready () even if they are added after the browser event has alr Eady occurred • Handles the event scheduling failed to allow scripts to delay it if necessary • Simulates a DOM ready event in some older browsers by checking for the existence of a DOM method that typically becomes available at the same time as the DOM when valid, use the local DOM interface of the browser, add window. onload () event processing as a security net. You can call $ (document). ready () multiple times and execute code in the order they are called. Execute the functions passed to $ (document). readu (), or even added after a browser event has occurred. Asynchronous processing of event calls allows delayed calling of scripts when required. By repeatedly checking the methods that exist at the same time when the DOM exists, we can simulate the events completed by DOM loading in the old browser. The. ready () method's parameter can accept a reference to an already defined function, as shown in the following code snippet: function addHighlightClass () {$ ('div. poem-stanza '). addClass ('highlight');} $ (document ). ready (addHighlightClass );. the sweet potato of the ready () method can be referenced by a defined function, as shown in the following code snippet: function addHighlightClass () {$ ('div. poem-stanza '). addClass ('highlight');} $ (document ). ready (addHighlightClass); Howe Ver, as demonstrated in the original version of the script, and repeated in Listing 1.2, as follows, the method can also accept an anonymous function (sometimes also called a lambda function ), as follows: $ (document ). ready (function () {$ ('div. poem-stanza '). addClass ('highlight');}); however, as shown in the original code of this script, as repeated in summary 1.2, this method can also accept an anonymous function (sometimes called a lambda function), as follows: $ (document ). ready (function () {$ ('div. poem-s Tanza '). addClass ('highlight');}); This anonymous function idiom is convenient in jQuery code for methods that take a function as an argument when that function isn' t reusable. moreover, the closureit creates can be an advanced and powerful tool. however, it may also have unintended consequences and ramifications on memory use, if not dealt with carefully. the topic of closures is discussed fully I N Appendix A, JavaScript Closures. Anonymous functions are very convenient for jquery code when functions are used as parameters and do not need to be reused. In addition, the closure he created can become an advanced and powerful tool. However, if you are not careful with it, you may generate unplanned results and scores in memory usage. The closure topic will be carefully discussed in the Appendix A JavaScript Closures chapter. The finished product Now that our JavaScript is in place, the page looks similar to the following screenshot: Now our js Code has been placed, the webpage looks like the following: The poem stanzas are now italicized and enclosed in boxes, as specified by the 01.css stylesheet, due to the insertion of the highlightclass by the JavaScript code.the display content is now skewed and framed, as defined in 01.css, due to the highlight class inserted through js Code.