Detailed explanation of jQuery method attributes

Detailed explanation of jQuery method attributes

1. Introduction to jQuery

What is jQuery and what does it do?

  • jQuery is used to simplify JS operations on DOM elements
  • jQuery cannot use DOM methods, and DOM cannot use jQuery methods

Usage characteristics of various selectors:

There are five basic selectors: $(" .#*,空格");

There are 4 types of relational selectors: $(“空格>+~ ”)

There are 8 basic filter selectors: $(" :first/:last/:even/:odd/eq(index)/:gt(index)/:lt(index)/:not(selector) ")

There are 4 content filter selectors: $(" :contains(text)/:empty/:has(selector)/:parent ")

There are 2 types of visibility filter selectors: $(" :hidden/:visible ")

There are 8 attribute selectors: ( " = = [ attribute ] , [ attribute = value ] , [ attribute ! = value ] , [ attribute = value ] , [ attribute ("==[attribute] , [attribute=value] [attribute!=value] , [attribute^=value]、[attribute ("==[attribute] , [attribute=value] [attribute!=value] [attribute=value] , [attribute=value] , [attribute*=value] , [attributeFilter1][attrbuteFilter2]==")

Sub-element filter selector (4 types) $( ":nth-child(index/even/odd) , :first-child , :last-child , :only-child" )

Form attribute filter selector (4 types) ${" :enabled/:disabled/:checked/:selected "}

Form selector (11 types) $(" :input/:text/:password/:radio/:checkbox/:submit/:image/:reset/:button/:file/:hidden ")

2. jQuery selector

2.1 Five basic selectors

    // 5 basic selectors $(".div"); // class selector $("div"); // tag selector $("#box"); // id selector $("*"); // wildcard selector $("div,p,img"); // merge selector

2.2 Four types of relationship selectors

    //4 types of relationship selectors $("div p"); //descendant selector $("div>p"); //child selector $("div+p"); //direct sibling selector $("div~p"); //simple sibling selector

2.3 8 basic filter selectors

// 8 basic filter selectors $(":first");//First element $(":last");//Last element $(":not(selector)");//Exclude selector $(":even");//Select even rows $(":odd");//Select odd rows $(":eq(index)");//Elements with index equal to index $(":gt(index)");//Elements with index greater than index $(":lt(index)");//Elements with index less than index

2.4 4 types of content filter selectors

        // 4 types of content filter selectors $(":contains(text)"); //Matches elements that contain the given text $(":empty"); //Matches all empty elements that do not contain child elements or text $(":has(selector)"); //Matches elements that contain the selector $(":parent"); //Matches elements that have child elements or text

2.5 Two types of visibility filter selectors

        // 2 types of visibility filter selectors $(":hidden"); // matches all invisible elements, or elements of type hidden $(":visible"); // matches all visible elements

2.6 8 types of attribute filter selectors

        // 8 types of attribute filter selectors $("[attribute]"); //Match elements with attribute attributes $("[attribute=value]"); //Match elements with attribute values ​​equal to value $("[attribute!=value]"); //Match elements with attribute values ​​not equal to value $("[attribute^=value]"); //Match elements with attribute values ​​starting with certain values ​​$("[attribute$=value]"); //Match elements with attribute values ​​ending with certain values ​​$("[attribute*=value]"); //Match elements with attribute values ​​containing certain values ​​$("[attributeFilter1][attrbuteFilter2]"); //Compound attribute filter selector, used when multiple conditions need to be met at the same time

2.7 Sub-element filter selectors (4 types)

        //Sub-element filter selector (4 types)
            $(":nth-child(index/even/odd)") //Select the index-th child element under each parent element $(":first-child"); //Select the first child element of each parent element $(":last-child"); //Select the last child element of each parent element $(":only-child"); //If an element is the only child element of its parent element, it will be matched

2.8 Form attribute filter selectors (4 types)

        //Form attribute filter selector (4 types)
            $(":enabled");//Select all enabled elements$(":disabled");//Select all disabled elements$(":checked");//Select all selected elements$(":selected");//Select all selected elements

2.9 Form selectors (11 types)

        // Form selector (11 types)
            $(":input");//Select all input/textarrea/select/button elements$(":text");//Select all single-line text boxes$(":password");//Select all password boxes$(":radio");//Select all radio buttons$(":checkbox");//Select all check boxes$(":submit");//Select all submit buttons$(":image");//Select all image buttons$(":reset");//Select all reset buttons$(":button");//Select all buttons$(":file");//Select all upload fields$(":hidden");//Select all invisible elements

3. DOM manipulation in jQuery

3.1 Text Operation

        // Text operation $("p").html(); // Equivalent to p.innerHtml in DOM;
        $("p").text();//Equivalent to p.innerText in DOM;

3.2 Value Operations

        // Value operation $("input:eq(5)").val(); //Equivalent to input.value in DOM;
        $("input:eq(6)").val("aaa"); //Set attribute value

3.3 Attribute Operation

 		// Attribute operation $("#box").attr('name'); //Get the name attribute $("#box").attr('name',"aaa"); //Add the name attribute and value $("#box").removeAttr('name'); //Delete the name attribute $("#box").prop('checked'); //When getting a single attribute, use prop to get false and true

3.4 Class Operations

    	// Class operation $("#box").attr("class","");//Get and set $("#box").addClass("class","");//Append class name $("#box").removeClass("class","");//Remove class name $("#box").removeClass();//Remove all class names $("#box").toggleClass("main");//Switch main class name $("#box").hasClass("main");//Is there a certain class name

3.5 Style Operation

	//Style operation $("#box").css("color"); //Read CSS style value $("#box").css({"propertyname":"value","propertyname":"value"}); //Set multiple styles at the same time

4. Node Operation

4.1 Traversing nodes

		 //Traverse nodes$("#box").children();//Get child nodes$("#box").children("div");//Get div child nodes$("#box").prev();//Find a brother immediately above$("#box").prevAll();//Find all brothers immediately above$("#box").prevAll("div");//Find all div brothers immediately above$("#box").next();//Find a brother immediately below$("#box").nextAll();//Find all brothers immediately below$("#box").nextAll("div");//Find all div brothers immediately below$("#box").parent();//Find parent node

4.2 Filter Nodes

    //Filter nodes $("ul").find(".a");//Search $("ul li").filter(".a");//Filter

4.3 Create, Insert, and Delete

    // Create, insert, delete var lis=$("<li title='aaa'>aaa</li>");//Create//Internally add parent.append(lis);//Add to the end of the parent box parent.prepend(lis);//Add to the head of the parent box//Externally add box.after(lis);//Add after the box box.before(lis);//Add before the box//Delete DOM elements $("ul").remove();//Completely delete, delete both ul and li $("ul").empty();//Just clear the contents of ul, ul still exists $("li").remove(".one");//Delete class="one" in li

5. jQuery Events

    // jQuery event // Difference from JS // window.onload and $(document).ready()
            //Difference 1: The former is executed after the page is fully loaded, while the latter is executed after the DOM is loaded. The latter takes precedence over the former. //Difference 2: When the former appears multiple times, the last one will overwrite the previous one. When the latter appears multiple times, they will be merged. //Difference 3: Is there an abbreviation: window has no abbreviation, document has an abbreviation. //Abbreviation: $().ready(function(){...})
                // $(function(){....})
        //Event binding: $(selector).on(event type, callback function)
        $("ul li").on("click",function(){alert(1);});	
    // jQuery and Ajax
            // get method $.get(url,data,success(response,status,xhr),dataType);
            // post method $.post(url,data,success(data, textStatus, jqXHR),dataType);

image-20211030195950325

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • JS JQuery obtains data-* attribute value method analysis
  • Analysis of JQuery style and attribute setting methods
  • Detailed explanation of the properties and methods of jQuery event objects
  • Detailed explanation of jQuery prototype properties and prototype methods

<<:  Web Design Tutorial (5): Web Visual Design

>>:  How to modify the "Browse" button of the html form to upload files

Recommend

Tomcat uses Log4j to output catalina.out log

Tomcat's default log uses java.util.logging, ...

Detailed explanation of nginx request header data reading process

In the previous article, we explained how nginx r...

Troubleshooting the cause of 502 bad gateway error on nginx server

The server reports an error 502 when synchronizin...

Flex layout realizes left text overflow and omits right text adaptation

I want to achieve a situation where the width of ...

MySQL calculates the number of days, months, and years between two dates

The MySQL built-in date function TIMESTAMPDIFF ca...

Vue realizes web online chat function

This article example shares the specific code of ...

CSS example code to hide the scroll bar and scroll the content

Preface When the HTML structure of a page contain...

Summary of react basics

Table of contents Preface start React Lifecycle R...

How to enhance Linux and Unix server security

Network security is a very important topic, and t...

How many common loops do you know about array traversal in JS?

Preface As a basic data structure, arrays and obj...

Bootstrap 3.0 study notes buttons and drop-down menus

The previous article was a simple review of the B...

Vue3 + TypeScript Development Summary

Table of contents Vue3 + TypeScript Learning 1. E...

Graphical tutorial on installing CentOS 7.3 on VMWare

Illustrated CentOS 7.3 installation steps for you...

Pitfalls based on MySQL default sorting rules

The default varchar type in MySQL is case insensi...

How to calculate the value of ken_len in MySQL query plan

The meaning of key_len In MySQL, you can use expl...