Home Web Front-end JS Tutorial Summary of common raw JS selector usage methods_javascript skills

Summary of common raw JS selector usage methods_javascript skills

May 16, 2016 pm 04:52 PM
Selector

Common getElementById, getElementsByName, getElementsByTagName. But foreigners were not satisfied with these APIs, so they came up with getElementsByClassName. Later, jQuery selectors appeared little by little. Here we only talk about original js selection.

1.getElementById

This is the most commonly used selector, located by id:

Example:

var test=document.getElementById(" test").value;//Get the value of the element with id test in the document and assign it to test face changing

2.getElementsByName

Example:

var test= document.getElementByName("test");//Get the node of the element named test in the document and assign it to the test variable. At this time, the test variable is an array

3.getElementsByTagName

Example:

var test=document.getElementsByTagName("test");//Get the node of the element with class test in the document and assign it to test. At this time, the test variable is an array, and this selector is in Cannot be used in IE5, 6, 7, 8

4.getElementsByClassName

This selector cannot be found in the js API. If you want to use it, you must define the method yourself. The usual principle First, use getElementsByTagName("*") to retrieve all elements in the document, then traverse, use regular expressions to find matching elements, put them into an array and return them. There are many programmers on the Internet who have implemented this selector. Here are two examples:

(1) The Ultimate getElementsByClassName scheme, authored by Robert Nyman, was implemented in 2005. It can be seen that many things by foreigners have been implemented a long time ago Very far away.

Copy code The code is as follows:

//All three parameters are required, find one There are 5007 elements with the class name "cell" in the web page. IE8 lasts 1828 ~ 1844 milliseconds,
//IE6 is 4610 ~ 6109 milliseconds, FF3.5 is 46 ~ 48 milliseconds, opera10 is 31 ~ 32 milliseconds, and Chrome is 23~26 milliseconds,
//safari4 is 19 ~ 20 milliseconds
function getElementsByClassName(oElm, strTagName, strClassName){
var arrElements = (strTagName == "*" && oElm.all)? oElm. all :
oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
strClassName = strClassName.replace(/-/g, "\-");
var oRegExp = new RegExp("(^|\s)" strClassName "(\s|$)");
var oElement;
for(var i=0; i < arrElements.length; i ){
oElement = arrElements[i];
if(oRegExp.test(oElement.className)){
arrReturnElements.push(oElement);
}
}
return (arrReturnElements)
}

(2) Provided by Dustin Diaz (author of "JavaScript Design Patterns"), but the compatibility is not as good as the above and does not support IE5.
Copy code The code is as follows:

//The last two parameters are reliable, find a web page There are 5007 elements with the class name "cell". IE8 lasts 78 milliseconds, IE6 lasts 125~171 milliseconds
// FF3.5 is 42 ~ 48 milliseconds, opera10 is 31 milliseconds, Chrome is 22 ~ 25 milliseconds, safari4 is 18 ~ 19 milliseconds
var getElementsByClass = function(searchClass,node,tag) {
var classElements = new Array();
if ( node == null )
node = document;
if ( tag == null )
tag = '*';
var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp(" (^|\s)" searchClass "(\s|$)");
for (i = 0, j = 0; i < elsLen; i ) {
if ( pattern.test(els[ i].className) ) {
classElements[j] = els[i];
j ;
}
}
return classElements;
}

------------------------------------------------ -------------------------------------------------- -------------------------------------------------- ----

Note: this can represent the node of the current element.

-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------

The following are some commonly used methods to match knowledge points such as events:
Copy codeThe code is as follows:

//Submit the form with id test

document.getElementById("test").submit();

//Set the border of the element with id test to 2 pixels, solid, red

document.getElementById("test").style.border="2px solid red";

//The mouse moves or moves out of the element with id test, Change its background color

function test(){
document.getElementById("test").onmouseover=function(){document.getElementById("test2").style.backgroundColor="red"} ;
document.getElementById("test").onmouseout=function(){document.getElementById("test2").style.backgroundColor="blue"};
}

//Pop up The number of elements with the name test in the document

function test()
 {
 var test=document.getElementsByName("test");
 alert(test.length);
 }
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the identifier of the id selector in css What is the identifier of the id selector in css Sep 22, 2022 pm 03:57 PM

In CSS, the identifier of the id selector is "#". You can specify a specific style for the HTML element marked with a specific id attribute value. The syntax structure is "#ID value {attribute: attribute value;}". The ID attribute is unique and non-repeatable in the entire page; the ID attribute value should not start with a number. IDs starting with numbers will not work in Mozilla/Firefox browsers.

Use the :nth-child(n+3) pseudo-class selector to select the style of child elements whose position is greater than or equal to 3 Use the :nth-child(n+3) pseudo-class selector to select the style of child elements whose position is greater than or equal to 3 Nov 20, 2023 am 11:20 AM

Use the :nth-child(n+3) pseudo-class selector to select the style of child elements whose position is greater than or equal to 3. The specific code example is as follows: HTML code: &lt;divid="container"&gt;&lt;divclass="item"&gt ;First child element&lt;/div&gt;&lt;divclass="item"&

css pseudo-selector learning pseudo-class selector analysis css pseudo-selector learning pseudo-class selector analysis Aug 03, 2022 am 11:26 AM

In the previous article "Css Pseudo-Selector Learning - Pseudo-Element Selector Analysis", we learned about pseudo-element selectors, and today we take a closer look at pseudo-class selectors. I hope it will be helpful to everyone!

What to do if the javascript selector fails What to do if the javascript selector fails Feb 10, 2023 am 10:15 AM

The JavaScript selector fails because the code is not standardized. The solution is: 1. Remove the imported JS code and the ID selector method will be effective; 2. Just introduce the specified JS code before introducing "jquery.js".

From beginner to proficient: Master the skills of using is and where selectors From beginner to proficient: Master the skills of using is and where selectors Sep 08, 2023 am 09:15 AM

From beginner to proficient: Master the skills of using is and where selectors Introduction: In the process of data processing and analysis, the selector is a very important tool. Through selectors, we can extract the required data from the data set according to specific conditions. This article will introduce the usage skills of is and where selectors to help readers quickly master the powerful functions of these two selectors. 1. Use of the is selector The is selector is a basic selector that allows us to select the data set based on given conditions.

Do selectors in css include hypertext tag selectors? Do selectors in css include hypertext tag selectors? Sep 01, 2022 pm 05:25 PM

Not included. CSS selectors include: 1. Tag selector, which locates specific HTML elements through the element name of the HTML page; 2. Class selector, which locates specific HTML elements through the value of the class attribute of the HTML element; 3. ID selector, which Locate specific HTML elements through the value of the id attribute of the HTML element; 4. The wildcard selector "*" can refer to all types of tag elements, including custom elements; 5. The attribute selector uses the existing attribute name of the HTML element or attribute value to locate a specific HTML element.

In-depth analysis of is and where selectors: improving CSS programming level In-depth analysis of is and where selectors: improving CSS programming level Sep 08, 2023 pm 08:22 PM

In-depth analysis of is and where selectors: improving the level of CSS programming Introduction: In the process of CSS programming, selectors are an essential element. They allow us to select and style elements in an HTML document based on specific criteria. In this article, we will take a deep dive into two commonly used selectors namely: is selector and where selector. By understanding their working principles and usage scenarios, we can greatly improve the level of CSS programming. 1. is selector is selector is a very powerful choice

What are the wxss selectors? What are the wxss selectors? Sep 28, 2023 pm 04:27 PM

Wxss selectors include element selectors, class selectors, ID selectors, pseudo-class selectors, child element selectors, attribute selectors, descendant selectors and wildcard selectors. Detailed introduction: 1. Element selector, use the element name as the selector to select matching elements, use the "view" selector to select all "view" components; 2. Class selector, use the class name as the selector to select For elements with a specific class name, use the ".classname" selector to select elements with the ".classname" class name, etc.

See all articles