Tool function categories in jquery: 1. Operations on arrays and objects, such as the "$.each()" function to traverse arrays and "grep()" function data filtering; 2. String operations, such as " The $.trim()" function removes spaces; 3. Test operations, such as the "$.contains()" function to detect DOM nodes; 4. URL operations, such as the "$.proxy()" function binds the scope of the event; 5. Browser detection, such as obtaining the properties of the "$.browser" object.
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
Tool function in jquery
What is a tool function
In jQuery, a tool function refers to a tool function that is directly attached to a jQuery object and targets a jquery object The definition itself, that is, global functions, are collectively called tool functions, or Utilities functions. They have an obvious feature. Generally, they are called in the following format:
$.函数名()或jquery.函数()
Classification of tool functions
According to tool functions Different processing objects can be divided into the following categories:
Browser detection
Array and object operations
String operations
Test operations
- ##URL operations
Browser detection
In browser detection, it can be divided into detection of browser type and characteristics. The former obtains the name of the browser or Version information, the latter detects that the browser supports the standard W3C box model.Browser name or version information
Although jquery has good browser compatibility, sometimes program developers need to obtain browser-related information for Provides the user or program. In jquery, it can be obtained by accessing the properties of the $.browser object. The $.browser object is the jQuery.browser object, which is used to handle browser-related transactions. The properties of this object are as follows:Box model
The box model is an exclusive noun in CSS, used to describe various attributes in page settingsThe width and height of the W3C box model do not include padding and borderIE box The model width and height include padding and border##$.support.boxModel property. If true, it is the W3C box model, otherwise it is the IE box model.
Array and object operations1) Traverse the array
Use the $.each() tool function, not only To realize the traversal of elements in the page, you can also complete the traversal of the specified array. The syntax format of the call is as follows:
$.each(obj,fn(para1,para2))
The parameter obj represents the array or object to be traversed, and fn is the callback executed for each traversed element. Function, this function contains two parameters, para1 represents the serial number of the array or the properties of the object, and para2 represents the elements of the array and the properties of the object.
2) Traverse the object
$. In addition to traversing the array, the each() function can also traverse the object and obtain the properties and values of the object.
3) Data filtering
When operating an array, sometimes it is necessary to filter elements according to various conditions. Traditional JavaScript code will traverse the entire array and set the elements of the filtering rules during the traversal. When using grep() in jQuery, the format is as follows:
$.grep(array,function(elementOfArray,indexInArray),[invert])
Among them, the parameter array is the original array to be filtered, and two parameters can be set in the callback function fn, where elementOfArray is the element in the array and indexInArray is The serial number of the element in the array; in addition, the optional [invert] is a Boolean value, indicating whether to invert the result according to the rules of fn. The default value is false, which means not to invert. If it is true, it means to invert, that is, return Data contrary to the rules of callback function fn.
Conditions In the return of unloading fn, for example: ele >5 && index
4) Data change
Modify the selected elements in the array according to the specified conditions ,
$.map(array,fn(eleArr,indexArr))
Among them, the parameter array is the original array to be changed, and two parameters can be set in the callback function fn, where eleArr is the element in the array, and indexArr is the serial number of the element in the array.
5) Data search
In jQuery, if you want to search for an element in an array, you can use the utility function $.inArray(), which is equivalent to using indexOf( in JavaScript ) searches for a certain character in a string. In the tool function $.inArray(), if a specified element is found, the index number of the element in the array is returned, otherwise, a value of -1 is returned. The calling format is as follows:
$.inArray(value,array)
Among them, the parameter value represents the object to be searched, and array represents the array of search objects.
String operation#If you want to remove the spaces on the left and right sides of the characters, you can use the tool function $.trim().
$.trim(str)
Test operation
原始对象:即对象是否通过{}或 new Object()关键字创建
$.contains()函数
$.contains()函数用于检测在一个DOM节点中是否包含另外一个DOM节点,其语法如下:
$.contains(container,contained)
其中:参数container为Object,是一个DOM元素,作为容器,可以包含其他DOM元素;参数contained也是一个DOM是一个节点,可能被其他元素包含。整个函数返回一个布尔值,如果包含返回true,否则返回false。
URL操作
$.param(obj,[traditional])
其中:参数obj表示需要进行序列化的对象,该对象可以是数组、jQuery元素、普通对象;可选项参数[traditional],表示是否使用普通话的方式浅层序列化,该函数返回一个序列化,该函数返回一个序列化后的字符串。
$.extend()扩展工具函数
$.proxy()函数 改变绑定事件的作用域
视频教程推荐:jQuery视频教程
The above is the detailed content of What are the categories of tool functions in jquery?. For more information, please follow other related articles on the PHP Chinese website!

实现方法: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内部的开始处增加元素。

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

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

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

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


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
