jQuery对于数组元素操作主要提供了以下工具:
(1)数组和对象的例遍:jQuery.each(obj,callback)
通用例遍方法,可用于例遍对象和数组。回调函数拥有两个参数:第一个为对象的成员或数组的索引,第二个为对应变量或内容。如果需要退出 each 循环可使回调函数返回 false,其它返回值将被忽略。
(2)数组元素的过滤:jQuery.grep(array,callback,[invert])
使用过滤函数过滤数组元素。此函数至少传递两个参数:待过滤数组和过滤函数。过滤函数必须返回 true 以保留元素或 false 以删除元素。
(3)数组元素的查找:jQuery.inArray(value,array)
确定第一个参数在数组中的位置(如果没有找到则返回 -1 )。
(4)删除重复元素:jQuery.unique(array)
删除数组中重复元素。
下面的实例通过对象数组的过滤来实现获取或者删除指定属性为指定值的数组元素。
<script> <BR>/** <BR>* 从对象数组中删除属性为objPropery,值为objValue元素的对象 <BR>* @param Array arrPerson 数组对象 <BR>* @param String objPropery 对象的属性 <BR>* @param String objPropery 对象的值 <BR>* @return Array 过滤后数组 <BR>*/ <BR>function remove(arrPerson,objPropery,objValue) <BR>{ <BR>return $.grep(arrPerson, function(cur,i){ <BR>return cur[objPropery]!=objValue; <BR>}); <BR>} <BR>/** <BR>* 从对象数组中获取属性为objPropery,值为objValue元素的对象 <BR>* @param Array arrPerson 数组对象 <BR>* @param String objPropery 对象的属性 <BR>* @param String objPropery 对象的值 <BR>* @return Array 过滤后的数组 <BR>*/ <BR>function get(arrPerson,objPropery,objValue) <BR>{ <BR>return $.grep(arrPerson, function(cur,i){ <BR>return cur[objPropery]==objValue; <BR>}); <BR>} <BR>/** <BR>* 显示对象数组信息 <BR>* @param String info 提示信息 <BR>* @param Array arrPerson 对象数组 <BR>*/ <BR>function showPersonInfo(info,arrPerson) <BR>{ <BR>$.each(arrPerson, function(index,callback){ <BR>info+="Person id:" + arrPerson[index].id + " name:" + arrPerson[index].name+ " sex:"+ arrPerson[index].sex+" age:"+ arrPerson[index].age+"\r\t"; <BR>}); <BR>alert(info); <BR>} <BR>//测试数据 <BR>var arrPerson=new Array(); <BR>var person=new Object(); <BR>person.id=1; <BR>person.name="帅哥"; <BR>person.sex="男"; <BR>person.age=30; <BR>arrPerson.push(person); <BR>person=new Object(); <BR>person.id=2; <BR>person.name="美眉甲"; <BR>person.sex="女"; <BR>person.age=28; <BR>arrPerson.push(person); <BR>person=new Object(); <BR>person.id=3; <BR>person.name="美眉乙"; <BR>person.sex="女"; <BR>person.age=20; <BR>arrPerson.push(person); <BR>//测试删除 <BR>showPersonInfo("原始数组: \t",arrPerson); <BR>arrPerson=remove(arrPerson,"id",1); <BR>showPersonInfo("删除之后: \t",arrPerson); <BR>//测试获取 <BR>arrPerson=get(arrPerson,"id",3); <BR>showPersonInfo("只获取ID为3的元素:\r\t",arrPerson); <BR></script>

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。


Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

AI Hentai Generator
Générez AI Hentai gratuitement.

Article chaud

Outils chauds

SublimeText3 version anglaise
Recommandé : version Win, prend en charge les invites de code !

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Télécharger la version Mac de l'éditeur Atom
L'éditeur open source le plus populaire

MinGW - GNU minimaliste pour Windows
Ce projet est en cours de migration vers osdn.net/projects/mingw, vous pouvez continuer à nous suivre là-bas. MinGW : un port Windows natif de GNU Compiler Collection (GCC), des bibliothèques d'importation et des fichiers d'en-tête librement distribuables pour la création d'applications Windows natives ; inclut des extensions du runtime MSVC pour prendre en charge la fonctionnalité C99. Tous les logiciels MinGW peuvent fonctionner sur les plates-formes Windows 64 bits.

Dreamweaver Mac
Outils de développement Web visuel