Chain callsAfter the jQuery object calls any method (except the node relationship method), the method will have a return value, which is the jQuery object itself. This phenomenon provides us with convenience, and we can continue to call jQuery methods and properties on the execution results. That is, you can use the jQuery object to make continuous dot calls console.log($(this).css("background-color", "pink").html("hello")); In addition to the node relationship methods, after the execution of other methods of the jQuery object, the return value is the object itself, and you can continue to call the methods and properties of other jQuery objects in a chain. This simplifies code writing A small caseClick an element to make itself pink, its siblings yellow, its parent blue, its parent's siblings color, and its parent's siblings itself orange <style> * { margin: 0; padding: 0; } .box { width: 400px; height: 60px; border: 1px solid #000; margin-top: 2px; } .box p,.box h2 { float: left; width: 60px; height: 60px; margin-right: 20px; background-color: rgb(164, 247, 233); } </style> <!------------------------------------------------------------------> <body> <div class="box"> <p></p> <p></p> <p></p> <p></p> <h2>h2</h2> </div> <div class="box"> <p></p> <p></p> <p></p> <p></p> <h2>h2</h2> </div> <div class="box"> <p></p> <p></p> <p></p> <p></p> <h2>h2</h2> </div> <div class="box"> <p></p> <p></p> <p></p> <p></p> <h2>h2</h2> </div> <script src="../jq/jquery-1.12.4.min.js"></script> <script> var $p = $("p"); var $box = $(".box") $p.click(function () { // Chain call implementation $(this).css("background-color","pink") //Change itself to pink.siblings().css("background-color","yellow") //Own brothers change to yellow.parent().css("background-color","skyblue") //And its parent changes to blue.siblings().css("background-color","lightgreen") //The brothers of the parent change to light green.children().css("background-color","orange") //The brothers of the parent change to orange}) The above is the detailed explanation of jQuery chain call. For more information about jQuery chain call, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Tutorial diagram of building a Hadoop high-availability cluster based on ZooKeeper
>>: Multiple ways to calculate age by birthday in MySQL
Background of the accident: A few days ago, due t...
Table of contents Problem Description Solution Pr...
We can create jsx/tsx files directly The project ...
Preface In front-end programming, we often use th...
Background Information I've been rereading so...
In HTML, <, >, &, etc. have special mean...
introduction You must have encountered this in an...
Installation & Configuration The official web...
Table of contents 1. Relationship between parent ...
Table of contents Problem Analysis Why encapsulat...
[LeetCode] 178.Rank Scores Write a SQL query to r...
Command: mysqlhotcopy This command will lock the ...
Recently, when I was writing web pages with PHP, I...
Table of contents 1. v-if 2. Use v-if on <temp...
background: I have done a project before, which r...