Example steps for implementing class selectors and name attribute selectors in js

Example steps for implementing class selectors and name attribute selectors in js

The emergence of jQuery has greatly improved our efficiency in operating DOM and has taken our development to a higher level. For example, jQuery's selector is a very powerful function. It includes class selectors, id selectors, attribute selectors, element selectors, level selectors, content filter selectors, etc. It is very convenient and fast, and these selectors have good compatibility. It can be said that operating DOM with jq selectors is fun, and it will be fun all the time! However, the emergence of the three major frameworks, Vue, React, and Angular, has greatly reduced the frequency of use of JQuery, and JQuery does have certain performance issues and various pitfalls when operating DOM and binding data, but it still cannot erase the powerful existence of JQ in operating DOM!

Having said so much about the awesomeness of JQuery, how are many of its internal principles implemented? Today we will simply implement a class selector and name attribute selector similar to jQuery.

Class selector:

function getElementsByClass(className) {
   var classArr = [];
   var tags = document.getElementsByTagName("*");

   for (var i = 0; i < tags.length; i++) {
    if (tags[i].nodeType == 1) {
     if (tags[i].getAttribute("class") == className) {
      classArr.push(tags[i]);
     }
    }
   }
   return classArr;
}

In fact, the name attribute selector is the same as the class selector, but the judgment condition is slightly changed:

function getElementsByName(name) {
   var nameArr = [];
   var num = 0;
   var tags = document.getElementsByTagName("*");

   for (var i = 0; i < tags.length; i++) {
    if (tags[i].nodeType == 1) {
     if (tags[i].getAttribute("name") == name) {
      nameArr.push(tags[i]);
     }
    }
   }

   return nameArr;
}

The name attribute selector is mostly used in form operations.

There is a nodeType attribute in the above code, which is used to determine the type of node. There are 12 values ​​for nodeType, 1 represents node element, 2 represents attribute, and 3 represents text content in element or attribute. These three values ​​are used more frequently, while the other 9 are not used much. If you want to know more, you can take a look at the API. Here, we need to get the element node, so we will determine whether the nodeType of the current element is 1.

Here is another way to use recursion to get all child nodes (including grandchild nodes) of an element:

  /** 
   * Recursively get all child nodes * 
   Node represents the parent node type value for which you want to obtain all child nodes:
   1 Element represents an element 2 Attr represents an attribute 3 Text represents the text content in an element or attribute 4 CDATASection represents a CDATA section in a document (text that will not be parsed by the parser)
   5 EntityReference represents entity reference 6 Entity represents entity 7 ProcessingInstruction represents processing instruction 8 Comment represents comment 9 Document represents the entire document (the root node of the DOM tree)
   10 DocumentType provides an interface to entities defined for a document 11 DocumentFragment represents a lightweight Document object that can hold a portion of a document 12 Notation represents a symbol declared in a DTD */
  var allChildNodes = function (node, type) {
   // 1. Create an array of all nodes var allCN = [];

   // 2. Recursively get all nodes var getAllChildNodes = function (node, type, allCN) {
    // Get all child nodes of the current element
    var nodes = node.childNodes;
    // Get the child nodes of nodes for (var i = 0; i < nodes.length; i++) {
     var child = nodes[i];
     // Determine whether it is a node of the specified type if (child.nodeType == type) {
      allCN.push(child);
     }
     getAllChildNodes(child, type, allCN);
    }
   }
   getAllChildNodes(node, type, allCN);
   // 3. Return the array of all nodes return allCN;
  }

  // Call:
  // Get all nodes in body allChildNodes(document.querySelector('body'), 1);
  
  //Get all the plain text nodes in body allChildNodes(document.querySelector('body'), 3)

Author: Xiaohuai

Source: http://tnnyang.cnblogs.com

The above is the details of the example steps for implementing class selectors and name attribute selectors in js. For more information about implementing class selectors and name attribute selectors in js, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • js implements circular menu selector
  • A general discussion on JS logical judgment selectors || &&
  • Understanding Selectors in JavaScript
  • Use iView date picker in Vue.js and set the start time and end time verification function
  • Vue2.0.js multi-level linkage selector implementation method
  • A brief discussion on the common selectors, attributes and method calls in JS
  • Javascript encapsulation id, class and element selector method example
  • JS imitates JQuery selector function
  • A brief discussion on the use of $(#ID) as a selector in js (when id is repeated)
  • js implements a complete example of a date picker accurate to seconds

<<:  Detailed explanation of basic operation commands such as starting and stopping Nginx under Windows

>>:  MySQL foreign key constraint disable and enable commands

Recommend

Text mode in IE! Introduction to the role of DOCTYPE

After solving the form auto-fill problem discussed...

Introduction to the use of several special attribute tags in HTML

The following attributes are not very compatible w...

Install centos7 virtual machine on win10

1. Download VMware Workstation 64 version https:/...

Summary of some tips for bypassing nodejs code execution

Table of contents 1. child_process 2. Command exe...

Learning about UDP in Linux

Table of contents 1. Introduction to UDP and Linu...

Share 10 of the latest web front-end frameworks (translation)

In the world of web development, frameworks are ve...

Implementing a simple age calculator based on HTML+JS

Table of contents Preface Demonstration effect HT...

Detailed explanation of the use of Vue h function

Table of contents 1. Understanding 2. Use 1. h() ...

Detailed explanation of COLLATION examples in MySQL that you may have overlooked

Preface The string types of MySQL database are CH...

In-depth explanation of MySQL common index and unique index

Scenario 1. Maintain a citizen system with a fiel...

jQuery realizes the picture following effect

This article shares the specific code of jQuery t...

Detailed explanation of MySQL InnoDB index extension

Index extension: InnoDB automatically extends eac...

A brief analysis of CSS3 using text-overflow to solve text layout problems

Basic syntax The use of text-overflow requires th...

Linux uses if to determine whether a directory exists.

How to use if in Linux to determine whether a dir...