Detailed explanation of jQuery chain calls

Detailed explanation of jQuery chain calls

Chain calls

After 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 case

Click 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:
  • JQuery special effects and chain call operation examples
  • Python implements an example of chain calls similar to jQuery
  • A brief analysis of jQuery chain calls and show knowledge
  • A brief analysis of jQuery's chain call each function
  • A brief analysis of jQuery's chain call
  • Javascript chain call implementation code (refer to jquery)
  • Imitate the chain call of jQuery each function

<<:  Tutorial diagram of building a Hadoop high-availability cluster based on ZooKeeper

>>:  Multiple ways to calculate age by birthday in MySQL

Recommend

Solution to uninstalling Python and yum in CentOs system

Background of the accident: A few days ago, due t...

Why the disk space is not released after deleting data in MySQL

Table of contents Problem Description Solution Pr...

Details on how to write react in a vue project

We can create jsx/tsx files directly The project ...

Summarize the commonly used nth-child selectors

Preface In front-end programming, we often use th...

How to monitor Windows performance on Zabbix

Background Information I've been rereading so...

The most commonly used HTML escape sequence

In HTML, <, >, &, etc. have special mean...

Mysql transaction isolation level principle example analysis

introduction You must have encountered this in an...

Tutorial on building a zookeeper server on Windows

Installation & Configuration The official web...

How to implement parent-child component communication with Vue

Table of contents 1. Relationship between parent ...

Vue.js manages the encapsulation of background table components

Table of contents Problem Analysis Why encapsulat...

SQL implementation of LeetCode (178. Score ranking)

[LeetCode] 178.Rank Scores Write a SQL query to r...

How to backup and restore the mysql database if it is too large

Command: mysqlhotcopy This command will lock the ...

Solution to the garbled problem of web pages when the encoding is set to utf-8

Recently, when I was writing web pages with PHP, I...

Vue conditional rendering v-if and v-show

Table of contents 1. v-if 2. Use v-if on <temp...