This article is translated from jQuery Cookbook (O'Reilly 2009) 1.0 The jQuery Philosophy
The philosophy of jQuery is "Write less code, do more things". This philosophy can be divided into three concepts:
- Find elements with CSS selectors and manipulate them with jQuery methods
- Chaining multiple jQuery methods on a set of elements
- jQuery encapsulation and implicit traversal
Fully understanding these three concepts is crucial to writing jQuery code. Let’s take a closer look at these three concepts.
Find elements and operate on them
To be more precise, it is to locate a batch of elements in the DOM tree and then operate on the set of elements. For example, the following example: first hide a
<SPAN class=dec><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></SPAN><SPAN class=pln> </SPAN><SPAN class=tag><html></SPAN><SPAN class=pln> </SPAN><SPAN class=tag><head></SPAN><SPAN class=pln> </SPAN><SPAN class=tag><script</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>type</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"text/JavaScript"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>src</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"</SPAN><SPAN class=tag>></script></SPAN><SPAN class=pln> </SPAN><SPAN class=tag></head></SPAN><SPAN class=pln> </SPAN><SPAN class=tag><body></SPAN><SPAN class=pln> </SPAN><SPAN class=tag><div></SPAN><SPAN class=pln>old content</SPAN><SPAN class=tag></div></SPAN><SPAN class=pln> </SPAN><SPAN class=tag><script></SPAN><SPAN class=pln> </SPAN><SPAN class=com>//隐藏页面上所有的div元素</SPAN><SPAN class=pln> jQuery</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>'div'</SPAN><SPAN class=pun>).</SPAN><SPAN class=pln>hide</SPAN><SPAN class=pun>();</SPAN><SPAN class=pln> </SPAN><SPAN class=com>//更新所有div元素内的文本</SPAN><SPAN class=pln> jQuery</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>'div'</SPAN><SPAN class=pun>).</SPAN><SPAN class=pln>text</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>'new content'</SPAN><SPAN class=pun>);</SPAN><SPAN class=pln> </SPAN><SPAN class=com>//在所有的div元素上添加值为updatedContent的class属性</SPAN><SPAN class=pln> jQuery</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>'div'</SPAN><SPAN class=pun>).</SPAN><SPAN class=pln>addClass</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>"updatedContent"</SPAN><SPAN class=pun>);</SPAN><SPAN class=pln> </SPAN><SPAN class=com>//显示页面上所有的div元素</SPAN><SPAN class=pln> jQuery</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>'div'</SPAN><SPAN class=pun>).</SPAN><SPAN class=pln>show</SPAN><SPAN class=pun>();</SPAN><SPAN class=pln> </SPAN><SPAN class=tag></script></SPAN><SPAN class=pln> </SPAN><SPAN class=tag></body></SPAN><SPAN class=pln> </SPAN><SPAN class=tag></html></SPAN>
Let’s take a look at these four jQuery statements one by one:
- Hide all div elements on the page, making them invisible
- Replace the original text in the hidden div element with new text ('new content')
- Add a new class attribute value (updatedContent) to the div element
- Redisplay the div element on the page
The above example uses the jQuery function to find all
Chain call
When calling jQuery methods, according to the design of jQuery, these methods can be called in a chain. For example, only perform an element search once, and then perform a series of operations on the found elements. The previous code example can be rewritten as a JavaScript statement using chained calls.
Using chain calls, you can use the following code:
<SPAN class=com>//隐藏页面上所有的div元素</SPAN><SPAN class=pln> jQuery</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>'div'</SPAN><SPAN class=pun>).</SPAN><SPAN class=pln>hide</SPAN><SPAN class=pun>();</SPAN><SPAN class=pln> </SPAN><SPAN class=com>//更新所有div元素内的文本</SPAN><SPAN class=pln> jQuery</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>'div'</SPAN><SPAN class=pun>).</SPAN><SPAN class=pln>text</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>'new content'</SPAN><SPAN class=pun>);</SPAN><SPAN class=pln> </SPAN><SPAN class=com>//在所有的div元素上添加值为updatedContent的class属性</SPAN><SPAN class=pln> jQuery</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>'div'</SPAN><SPAN class=pun>).</SPAN><SPAN class=pln>addClass</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>"updatedContent"</SPAN><SPAN class=pun>);</SPAN><SPAN class=pln> </SPAN><SPAN class=com>//显示页面上所有的div元素</SPAN><SPAN class=pln> jQuery</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>'div'</SPAN><SPAN class=pun>).</SPAN><SPAN class=pln>show</SPAN><SPAN class=pun>();</SPAN>
is rewritten as:
<SPAN class=pln>jQuery</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>'div'</SPAN><SPAN class=pun>).</SPAN><SPAN class=pln>hide</SPAN><SPAN class=pun>().</SPAN><SPAN class=pln>text</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>'new content'</SPAN><SPAN class=pun>).</SPAN><SPAN class=pln>addClass</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>"updatedContent"</SPAN><SPAN class=pun>).</SPAN><SPAN class=pln>show</SPAN><SPAN class=pun>();</SPAN>
If you add code indentation it will be:
<SPAN class=pln>jQuery</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>'div'</SPAN><SPAN class=pun>)</SPAN><SPAN class=pln> </SPAN><SPAN class=pun>.</SPAN><SPAN class=pln>hide</SPAN><SPAN class=pun>()</SPAN><SPAN class=pln> </SPAN><SPAN class=pun>.</SPAN><SPAN class=pln>text</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>'new content'</SPAN><SPAN class=pun>)</SPAN><SPAN class=pln> </SPAN><SPAN class=pun>.</SPAN><SPAN class=pln>addClass</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>"updatedContent"</SPAN><SPAN class=pun>)</SPAN><SPAN class=pln> </SPAN><SPAN class=pun>.</SPAN><SPAN class=pln>show</SPAN><SPAN class=pun>();</SPAN>
Simply put, chained calls allow unlimited jQuery methods to be used together on the currently selected set of elements. In essence, elements processed with jQuery methods will always be returned after the method is processed, so the chain of calls can continue. The jQuery plug-in is also designed in this way (returning an encapsulated element set), so using the plug-in does not affect the chain call.
Another benefit of chained calls is that it saves overhead by selecting DOM elements only once. Avoiding traversing the DOM tree is crucial to improving web page performance, so it is necessary to reuse or cache the selected set of DOM elements as much as possible.
jQuery wrapper
In most cases, if you use jQuery, you will definitely deal with something called "jQuery encapsulation". In other words, elements selected from an HTML page using jQuery will be encapsulated with a layer of functionality provided by jQuery. I personally like to call this thing "encapsulated element set" because it is a set of elements that encapsulates jQuery functions. This set of encapsulated elements sometimes contains one DOM element, sometimes it contains multiple, and sometimes it contains nothing at all. When the set of encapsulating elements is empty, jQuery methods/properties called on it will not throw any errors - this avoids unnecessary if statements.
Using the above HTML code as an example, what happens when there are multiple
<SPAN class=dec><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></SPAN><SPAN class=pln> </SPAN><SPAN class=tag><html></SPAN><SPAN class=pln> </SPAN><SPAN class=tag><head></SPAN><SPAN class=pln> </SPAN><SPAN class=tag><script</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>type</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"text/JavaScript"</SPAN><SPAN class=pln> </SPAN><SPAN class=atn>src</SPAN><SPAN class=pun>=</SPAN><SPAN class=atv>"http://ajax.googleapis.com/ajax/libs/ jquery/1.3.0/jquery.min.js"</SPAN><SPAN class=tag>></script></SPAN><SPAN class=pln> </SPAN><SPAN class=tag></head></SPAN><SPAN class=pln> </SPAN><SPAN class=tag><body></SPAN><SPAN class=pln> </SPAN><SPAN class=tag><div></SPAN><SPAN class=pln>old content</SPAN><SPAN class=tag></div></SPAN><SPAN class=pln> </SPAN><SPAN class=tag><div></SPAN><SPAN class=pln>old content</SPAN><SPAN class=tag></div></SPAN><SPAN class=pln> </SPAN><SPAN class=tag><div></SPAN><SPAN class=pln>old content</SPAN><SPAN class=tag></div></SPAN><SPAN class=pln> </SPAN><SPAN class=tag><div></SPAN><SPAN class=pln>old content</SPAN><SPAN class=tag></div></SPAN><SPAN class=pln> </SPAN><SPAN class=tag><script></SPAN><SPAN class=pln> jQuery</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>'div'</SPAN><SPAN class=pun>).</SPAN><SPAN class=pln>hide</SPAN><SPAN class=pun>().</SPAN><SPAN class=pln>text</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>'new content'</SPAN><SPAN class=pun>).</SPAN><SPAN class=pln>addClass</SPAN><SPAN class=pun>(</SPAN><SPAN class=str>"updatedContent"</SPAN><SPAN class=pun>).</SPAN><SPAN class=pln>show</SPAN><SPAN class=pun>();</SPAN><SPAN class=pln> </SPAN><SPAN class=tag></script></SPAN><SPAN class=pln> </SPAN><SPAN class=tag></body></SPAN><SPAN class=pln> </SPAN><SPAN class=tag></html></SPAN>
In the above example there is no programming code representing a loop. But the wonderful thing is that jQuery will scan the entire page, then put all
Being familiar with encapsulated element sets and implicit traversal is very important for writing complex looping logic - it is important to note that before writing any additional looping code, a simple looping operation already exists (for example: jQuery(' div').each(function(){}). In other words, the call to the jQuery method affects every element in the encapsulated element set.
It should be noted that some jQuery methods have special behavior and will only affect the first element in the set of encapsulated elements (for example: attr()).
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

This article outlines ten simple steps to significantly boost your script's performance. These techniques are straightforward and applicable to all skill levels. Stay Updated: Utilize a package manager like NPM with a bundler such as Vite to ensure

Sequelize is a promise-based Node.js ORM. It can be used with PostgreSQL, MySQL, MariaDB, SQLite, and MSSQL. In this tutorial, we will be implementing authentication for users of a web app. And we will use Passport, the popular authentication middlew

This article will guide you to create a simple picture carousel using the jQuery library. We will use the bxSlider library, which is built on jQuery and provides many configuration options to set up the carousel. Nowadays, picture carousel has become a must-have feature on the website - one picture is better than a thousand words! After deciding to use the picture carousel, the next question is how to create it. First, you need to collect high-quality, high-resolution pictures. Next, you need to create a picture carousel using HTML and some JavaScript code. There are many libraries on the web that can help you create carousels in different ways. We will use the open source bxSlider library. The bxSlider library supports responsive design, so the carousel built with this library can be adapted to any
