What is jquery chain programming

WBOY
Release: 2022-06-24 16:58:30
Original
1688 people have browsed it

In jquery, chain programming refers to performing function operations on the same element; chain programming merges multiple lines of code into one line of code, and the result returned by each merged method is an element object. Chain programming can be performed, and the syntax is "element object.Method().Method().Method()...;".

What is jquery chain programming

The operating environment of this tutorial: windows10 system, jquery3.6.0 version, Dell G3 computer.

What is jquery chain programming

The core of chain programming is that every method in the object returns the current object.

Chain programming: Multiple lines of code are merged into one line of code. The premise is to recognize whether the code returned by this line of code is an object. Only if it is an object can chain programming be performed.

Chain programming: object. Method().Method().Method();......

1. Chain programming

In jQuery, if it has been To perform function operations on the same element, you can use . Function operation name and keep writing it.

2. Chain programming of commonly used binding event functions

Example:

				//这是普通的事件绑定
				$("button").click(function() {
					console.log("1")
				})

				$("button").mouseenter(function() {
					console.log("2")
				})

				$("button").mouseleave(function() {
					console.log("3")
				})

				//与上文功能相同的链式编程
				$("button").click(function() {
					console.log("1")
				}).mouseenter(function() {
					console.log("2")
				}).mouseleave(function() {
					console.log("3")
				})
Copy after login

The core of chain programming , is the this object returned after the function call ends, referring to the current caller. Here$("button").click(function(){})After the call is completed, this object is returned, which is equivalent to $("button"), this and the following together achieve the function call of $("button").mouseenter(function() {}). The above are the general steps for chain programming implementation.

3. Chain programming of on function

Example:

			//普通写法			$("#btn1").on("click",function(){				console.log("点击事件")			})			$("#btn1").on("mouseenter",function(){  //注意这里的on函数的链式编程				console.log("鼠标聚焦事件")			})			$("#btn1").on("mouseleave",function(){  //注意这里的on函数的链式编程				console.log("鼠标失焦事件")			})						//链式编程			$("#btn1").on("click",function(){				console.log("点击事件")			}).on("mouseenter",function(){  //注意这里的on函数的链式编程				console.log("鼠标聚焦事件")			}).on("mouseleave",function(){  //注意这里的on函数的链式编程				console.log("鼠标失焦事件")			})
Copy after login

The on function chain programming here, function After the call is completed, the this keyword will be returned implicitly, indicating the currently called object. After the first on function call is completed, the returned this keyword represents $("#btn1"), After all, adding the on function is a matter of course.

4. Bind function chain programming

Example:

				//普通写法
				$("button").bind({"click":function(){
					console.log("点击事件")
				}})
				$("button").bind({"mouseenter":function(){
					console.log("鼠标聚焦事件")
				}})
				$("button").bind({"mouseleave":function(){
					console.log("鼠标离焦事件")
				}})

				//链式编程
				$("button").bind({"click":function(){
					console.log("点击事件")
				},
				"mouseenter":function(){
					console.log("鼠标聚焦事件")
				},
				"mouseleave":function(){
					console.log("鼠标离焦事件")
				}})
Copy after login

The bind function chain programming here is Put multiple parameters in the bind function at the same time. This is because each parameter is stored in the form of a dictionary and has the same format, so it can be placed in the bind function as parallel parameters at the same time. You need to remember this format.

5. Mixed use

Example:

	//混合使用
	$("button").bind({"click":function(){
					console.log("点击事件")
				}})
				$("button").bind({"mouseenter":function(){
					console.log("鼠标聚焦事件")
				}}).mouseleave(function(){
					console.log("混合使用的离焦事件")
				})
Copy after login

Run result:
What is jquery chain programming

Video Tutorial recommendation: jQuery video tutorial

The above is the detailed content of What is jquery chain programming. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template