


How does jquery traverse and obtain the list collection from the background?
In the project, jquery obtains the list from the background. How does it traverse it?
Under normal circumstances, the list in the background should be converted into a json string and returned to the callback function of ajax. The json string can be directly manipulated in the callback function.
For example:
$.post("test.php", { name: "John", time: "2pm" },
function(data){ //可以在这儿循环,比如: var listNow=data.listHouTai;//取list。listHouTai是你后台定义的json名称 for ( var i = 0; i < listNow.length; i++) { var id = vos[i].Id;//可以取list中第一个对象的id值,其他的类推 } });
You can use a jQuery custom array operation classjs external file, the prerequisite is to introduce the jQuery class library. The encapsulation class code is as follows:
(function ($) { $.List = function () { var _list = new Array(); //外部为数组赋值 this.GetDataSource = function (arr) { if (IsArrayType(arr)) { _list = arr; } else { alert("指定元素非数组类型,赋值失败!"); } }; //添加一个元素 this.Add = function (arg) { if (arg) { _list.push(arg); } else { alert("参数错误,添加元素失败!"); } return _list; }; //删除指定索引的元素 this.RemoveAt = function (index) { if (IsArrayIndex(index) && index < _list.length) { var i; var arr = new Array(); for (i = 0; i < _list.length; i++) { if (i != index) { arr.push(_list[i]); } } _list = arr; return _list; } else { alert("未获取到设置对象的实例,删除元素失败!"); } }; //按照指定的分割符显示出所有元素 this.Split = function (arg) { arg = arg || ","; var i, res; res = ""; if (_list.length > 0) { for (i = 0; i < _list.length; i++) { res += _list[i].toString() + arg; } return res.substr(0, (res.length - arg.toString().length)); } else { return ""; } }; //外部调用直接返回当前数组实力 this.ToArray = function () { return _list; }; //设置指定索引处的值为指定值 this.Update = function (index, value) { if (IsArrayIndex(index) && index < _list.length) { _list[index] = value; } return _list; }; //清空所有元素 this.RemoveAll = function () { _list.splice(0, _list.length); return _list; }; //根据传入的值获取第一次出现在数组中的下标 this.IndexOf = function (value) { if (value) { var i; for (i = 0; i < _list.length; i++) { if (_list[i] == value) { return i; } } } return -1; }; //获取数组长度 this.Size = function () { return _list.length; }; //移除数组中重复的项 this.RemoveRepeat = function () { _list.sort(); var rs = []; var cr = false; for (var i = 0; i < _list.length; i++) { if (!cr) cr = _list[i]; else if (cr == _list[i]) rs[rs.length] = i; else cr = _list[i]; } for (var i = rs.length - 1; i >= 0; i--) this.RemoveAt(rs[i]); return _list; }; //对数字数组元素排序,参数:0升序1降序 this.SortNumber = function (f) { if (!f) f = 0; if (f == 1) return _list.sort(function (a, b) { return b - a; }); return _list.sort(function (a, b) { return a - b; }); }; //私有方法 //判断正确的数组下标 function IsArrayIndex(index) { var reg = /^\d+$/; if (reg.test(index)) return true; else return false; } //判断当前对象是否为数组对象 function IsArrayType(arr) { if (typeof arr == 'object' && typeof arr.length == 'number') return true; else return false; } }; //结束List的构造方法 })(jquery);
Two js files need to be introduced when calling the page:
<script src="js/jquery-1.8.3.min.js" type="text/JavaScript"></script> <script src="js/jquery.array.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { var myList = new $.List(); myList.Add(1); myList.Add("1906-07-08"); myList.Add("hellow world"); myList.RemoveAt(0); myList.Update(0, "11111111"); //alert("数组被修改内容后的结果:" + myList.Split("|")); myList.RemoveAll(); var arr = myList.ToArray(); //alert("数组全部被删除后结果:" + arr); //alert("数组1当前长度:" + myList.Size()); var myList2 = new $.List(); myList2.Add(3); myList2.Add(1); myList2.Add(45); myList2.Add(21); myList2.Add(-9); myList2.Add(1); alert("第二个实例数组结果:" + myList2.ToArray()); myList2.RemoveRepeat(); alert("去重后第二个实例数组结果:" + myList2.ToArray()); alert("去重后第二个实例数组长度:" + myList2.Size()); myList2.SortNumber(1); alert("排序后的数组:" + myList2.ToArray()); var arr3 = ["aaa", "bbb", "ccc", "ddd", "eee"]; var arr4; myList2.GetDataSource(arr3); alert("重新赋值后结果:"+myList2.ToArray()); }); </script>
Object syntax JSON data format (when the server calls back The object data format is json data format, and the format requirements of JSON must be met. The callback object must be converted using the eval function (otherwise, the Object will not be obtained). This article will not introduce the data issues of the server-side callback in detail. We will directly customize the object. )
1.jqueryTraverse the object
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML > <HEAD > <TITLE > New Document < /TITLE> <script language="javascript" type="text/javascript " src="jquery.min.js "></script> <script type="text / javascript "> $(function(){ var tbody = ""; //------------遍历对象 .each的使用------------- var obj =[{"name ":"项海军","password ":"123456 "}]; $("#result ").html("------------遍历对象.each的使用-------------"); alert(obj);//是个object元素 //下面使用each进行遍历 $.each(obj,function(n,value) { alert(n+' '+value); var trs = ""; trs += " < tr > <td > " + value.name +" < /td> <td>" + value.password +"</td > </tr>"; tbody += trs; }); $("#project").append(tbody); }); </script > </HEAD> <BODY> <div id="result" style="font-size:16px;color:red;"></div > <table cellpadding = 5 cellspacing = 1 width = 620 id = "project"border = "1" > <tr > <th > 用户名 < /th> <th>密码</th > </tr> </table > </BODY> </HTML >
2.jQueryTraverse the array
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML > <HEAD > <TITLE > New Document < /TITLE> <script language="javascript" type="text/javascript " src="jquery.min.js "></script> <script type="text / javascript "> $(function(){ var tbody = ""; //------------遍历数组 .each的使用------------- var anArray = ['one','two','three']; $("#result ").html("------------遍历数组.each的使用-------------"); $.each(anArray,function(n,value) { alert(n+' '+value); var trs = ""; trs += " < tr > <td > " +value+" < /td></tr > "; tbody += trs; }); $("#project ").append(tbody); }); </script> </HEAD> <BODY> ------------此部分同1中的body部分-------------------- </BODY> </HTML> 3.jQuery 遍历List集合(其实与遍历一个对象没有太大区别,只是格式上的问题) <!DOCTYPE HTML PUBLIC " - //W3C//DTD HTML 4.0 Transitional//EN"> < HTML > <HEAD > <TITLE > New Document < /TITLE> <script language="javascript" type="text/javascript " src="jquery.min.js "></script> <script type="text / javascript "> $(function(){ var tbody = ""; //------------遍历List集合 .each的使用------------- var obj =[{"name ":"项海军","password ":"123456 "},{"name ":"科比","password ":"333333 "}]; $("#result ").html("遍历List集合.each的使用"); alert(obj);//是个object元素 //下面使用each进行遍历 $.each(obj,function(n,value) { alert(n+' '+value); var trs = ""; trs += " < tr > <td > " +value.name+" < /td> <td>" + value.password +"</td > </tr>"; tbody += trs; }); $("#project").append(tbody); }); </script > </HEAD> <BODY> ------------此部分同1中的body部分-------------------- </BODY > </HTML>
The above is the detailed content of How does jquery traverse and obtain the list collection from the background?. 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
