How to use $.each in JQuery and the difference with $(selector).each()
Theeach() method can make the DOM loop structure concise and less error-prone. The each() function encapsulates a very powerful traversal function and is very convenient to use. It can traverse one-dimensional array, multi-dimensional array, DOM, JSON, etc. During the javaScript development process Using $each can greatly reduce our workload.
each() method example:
var arr = [ "aaa", "bbb", "ccc" ]; $.each(arr, function(i,a){ alert(i); // i 是循环的序数 alert(a); // a 是值 }); var arr1 = [[1, 4, 3], [4, 6, 6], [7, 20, 9]] $.each(arr1, function(i, item){ alert(item[0]); });
In fact, arr1 is a two-dimensional array, item is equivalent to taking each one-dimensional array,
item[0] Relative to taking the first value in each one-dimensional array
So the output of each above is: 1 4 7
A general traversal function that can be used to traverse objectsAnd arrays. Arrays and pseudo-array objects containing a length attribute (pseudo-array objects such as the arguments object of a function) are traversed with a numerical index, from 0 to length-1, and other objects are traversed through the attributes.
$.each() is different from $(selector).each(). The latter is specially used for traversing jquery objects. The former can be used to traverse any collection (whether it is an array or an object). If it is an array, Callback The function passes in the index of the array and the corresponding value each time (the value can also be obtained through the this keyword, but javascript always wraps this value as an object - even if it is a string or a number), the method will return the first parameter of the traversed object.
Example:————Pass in an array
<!DOCTYPE html> <html> <head> <script src=”http://code.jquery.com/jquery-latest.js”></script> </head> <body> <script> $.each([52, 97], function(index, value) { alert(index + ‘: ‘ + value); }); </script> </body> </html>
//输出 0: 52 1: 97
Example:————If a map is used as a collection, the callback function passes in one key at a time -Value pair
<!DOCTYPE html> <html> <head> <script src=”http://code.jquery.com/jquery-latest.js”></script> </head> <body> <script> var map = { ‘flammable': ‘inflammable', ‘duh': ‘no duh' }; $.each(map, function(key, value) { alert(key + ‘: ‘ + value); }); </script> </body> </html>
//输出 flammable: inflammable duh: no duh
Example:——You can exit $.each() when returning false in the callback function. If a non-false is returned, it will be like in the for loop# The same as using continue in ##, it will immediately enter the next traversal
<!DOCTYPE html> <html> <head> <style> p { color:blue; } p#five { color:red; } </style> <script src=”http://code.jquery.com/jquery-latest.js”></script> </head> <body> <p id=”one”></p> <p id=”two”></p> <p id=”three”></p> <p id=”four”></p> <p id=”five”></p> <script> var arr = [ "one", "two", "three", "four", "five" ];//数组 var obj = { one:1, two:2, three:3, four:4, five:5 }; // 对象 jQuery.each(arr, function() { // this 指定值 $(“#” + this).text(“Mine is ” + this + “.”); // this指向为数组的值, 如one, two return (this != “three”); // 如果this = three 则退出遍历 }); jQuery.each(obj, function(i, val) { // i 指向键, val指定值 $(“#” + i).append(document.createTextNode(” – ” + val)); }); </script> </body> </html>
// 输出 Mine is one. – 1 Mine is two. – 2 Mine is three. – 3 - 4 - 5
Example:————Traverse the items of the array, passing in index and value
<!DOCTYPE html> <html> <head> <script src=”http://code.jquery.com/jquery-latest.js”></script> </head> <body> <script> $.each( ['a','b','c'], function(i, l){ alert( “Index #” + i + “: ” + l ); }); </script> </body> </html>
Example:——Traverse the properties of the object and pass in the key and value
<!DOCTYPE html> <html> <head> <script src=”http://code.jquery.com/jquery-latest.js”></script> </head> <body> <script> $.each( { name: “John”, lang: “JS” }, function(k, v){ alert( “Key: ” + k + “, Value: ” + v ); }); </script> </body> </html>
Example from the comment
1. If you don’t want to output the first item (use retrun true) and enter the next iteration
<!DOCTYPE html> <html> <head> <script src=”http://code.jquery.com/jquery-latest.js”></script> </head> <body> <script> var myArray=["skipThis", "dothis", "andThis"]; $.each(myArray, function(index, value) { if (index == 0) { return true; // equivalent to ‘continue' with a normal for loop } // else do stuff… alert (index + “: “+ value); }); </script> </body> </html>
The above is the detailed content of How to use $.each in JQuery and the difference with $(selector).each(). For more information, please follow other related articles on the PHP Chinese website!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
