JQuery - about jQuery operation properties, elements, size locations, etc.

Keywords: Javascript Attribute JQuery

1. Operations on attributes

The so-called attribute operation is to manipulate the attributes of a series of elements, value la class.... Especially the operation on input is very important.

To complete the next advanced Sao operations on the framework, let's now take a look at the three prop(), attr(), and data() operations common to jQuery attributes.

(1) Property value prop() inherent to the element

  • So-called intrinsic attributes, such as href input type in tag a, etc.
    Let's see how to get it and how to set it up
  1. Get: prpo('attribute')
  2. Set property: prpo('property','property value')
  3. Perfect properties for manipulating forms such as disabled/checked/selected

(2) Custom attribute value attr()

  • Used to add self-defined attributes to an element
  1. Get: attr('attribute');
  2. Settings: attr('attribute','attribute value')
    Note: Similar to native DOM>>getAttribute(), this applies to H5 custom attributes

(3) data related / very important!/

  • This is very important!Especially when it comes to later operation data rendering, and when there is a framework, similar implementation logic is used in all three front-end frameworks
  1. Syntax for attaching (setting) data: data ('name','value')
  2. The syntax for getting data('name').
    Note: The same applies to getting custom attributes in H5
    Code examples:
 <a href="http://Www.baidu.com "title=" are all good "> are all good </a>
    <input type="checkbox" name="" id="" checked>
    <div index="1" data-index="2">I am div</div>
        $(function() {
            //1. element.prop("attribute name") Gets the intrinsic attribute value of an element
            $("a").prop("title", "We're all fine");
            $("input").change(function() {
            // console.log($("div").prop("index"));
            // 2. Element's custom attributes We pass attr()
            $("div").attr("index", 4);
            // 3. Data Cache data() This data is stored in the element's memory
            $("span").data("uname", "andy");
            // This method gets the data-index h5 custom property The first one does not write data-and returns a numeric type

2. Operations on text attributes

There are three main types of text operations: html() / text()/ val(); attributes of innerHTML, innerText, and value corresponding to element DOM

These operations are not difficult, see the code examples below for more details

        <span>I am content</span>
    <input type="text" value="Please enter content">
        // 1. Get the setting element content html()
        // $("div").html("123");
        // 2. Get text() for setting element text content
        // 3. Get the set form value val()

3. Operations on elements

The main operations on elements are: traversing tags, creating tags, adding tags, deleting tags, etc. (A simple understanding is to check tags for additions and deletions)

(1) Traversing elements

  • jQuery implicit iteration does the same thing for the same type of element.Traversal is required if you want to do different things for the same type of element.
  1. There are two kinds of grammar
    Grammar 1

Note: This method is used to iterate through each item in a jQuery object. The element in the callback function is a DOM object and conversion is required to use the jQuery method.

Grammar 2

Note: This method is used to iterate through each item in a jQuery object. The element in the callback function is a DOM object and conversion is required to use the jQuery method.

  1. Code examples:
        $(function() {
            // Traversal elements are required if different operations are performed on the same type of element (similar to for, but more powerful than for)
            var sum = 0;
            var arr = ["red", "green", "blue"];
            // 1. each() method traverses elements 
            $("div").each(function(i, domEle) {
                // The first parameter of the callback function must be the index number. You can specify the name of the index number yourself.
                // console.log(i);
                // The second parameter of the callback function must be a dom element object, named after itself
                // console.log(domEle); //Use of jQuery method requires conversion $(domEle)
                $(domEle).css("color", arr[i]);
                sum += parseInt($(domEle).text());
            // 2. $.each() method traversal element is mainly used to traverse data and process data
            // $.each($("div"), function(i, ele) {
            //     console.log(i);
            //     console.log(ele);
            // });
            // $.each(arr, function(i, ele) {
            //     console.log(i);
            //     console.log(ele);
            // })
                name: "andy",
                age: 18
            }, function(i, ele) {
                console.log(i); // The output is the name age attribute name
                console.log(ele); // The output is an Andy 18 attribute value

(2) Create, modify, add

  • They are very simple to operate on. Check the official API for more details

The jQuery method operates on elements that can be created, added, or deleted in a number of ways, with emphasis on the following:

Grammar Sum

Tell me a little more: these are just common ways to create, add, and delete elements. For other ways, see the API.

Case Code

        <li>Original li</li>
    <div class="test">I was the original div</div>
        $(function() {
            // 1. Create Elements
            var li = $("<li>I created it later li</li>");
            // 2. Add Elements
            // 	2.1 Internal Add
            // $("ul").append(li); added internally and placed at the end of the content 
            $("ul").prepend(li); // Add internally and put at the top of the content
            //  2.2 External Add
            var div = $("<div>I was born to a stepmother</div>");
            // $(".test").after(div);
            // 3. Delete elements
            // $("ul").remove(); can delete matching elements suicide
            // $("ul").empty(); //can delete child nodes inside matching elements
            $("ul").html(""); // Child nodes inside matching elements can be deleted

4. Operations on size position

(1) Operations on elements (dimensions)//Hehehey is driving, dimensions

  • Grammatical Explanation

Note: With this set of API s, we will be able to quickly get the width and height of the sum. For other attributes we want to get and set, we also need to use css() and other methods to work together.

  • Specific sample code:
        $(function() {
            // 1. width() / height() Gets the size of the setting element width and height 
            // $("div").width(300);

            // 2. innerWidth() / innerHeight() Gets the size of the setting elements width and height + padding 

            // 3. outerWidth() / outerHeight() Gets the size of the setting elements width and height + padding + border 

            // 4. outerWidth(true) / outerHeight(true) get settings width and height + padding + border + margin

(2) Operations on location

The three main operations are offset()/ position()/ scrollTop()/ scrollLeft()/.... Of course, there are more sauce operations.

  • As usual, let's look at the grammar first

  • Code Instances
    <div class="father0">
        <div class="son"></div>
    <div class="back">Return to top</div>
    <div class="container"></div>
        $(function() {
            // 1. Get offset to set the distance from the document
            // $(".son").offset({
            //     top: 200,
            //     left: 200
            // });
            // 2. Get distance with positioned parent location (offset) positionIf there is no positioned parent, the document prevails
            // This method can only get offsets that cannot be set
            // $(".son").position({
            //     top: 200,
            //     left: 200
            // });
      		// 3. Curled head
            // scrollTop() of the curled head / left scrollLeft() of the curled head
            // Page Scroll Event
            var boxTop = $(".container").offset().top;
            $(window).scroll(function() {
                // console.log(11);
                if ($(document).scrollTop() >= boxTop) {
                } else {
            // Return to top
            $(".back").click(function() {
                // $(document).scrollTop(0);
                $("body, html").stop().animate({
                    scrollTop: 0
                // $(document).stop().animate({
                //     scrollTop: 0
                // }; can't be a document but an html and body element to animate

Posted by Elemen7s on Sun, 22 Mar 2020 19:46:56 -0700