Home >Web Front-end >Front-end Q&A >What does javascript use to get dom elements?

What does javascript use to get dom elements?

青灯夜游
青灯夜游Original
2022-09-29 16:36:195455browse

Javascript method to get dom elements: 1. Use getElementById() to get the element based on the id; 2. Use getElementsByName() to get the element based on the name attribute; 3. Use getElementsByTagName() to get the element based on the tag name; 4. Use getElementsByClassName() to get elements based on the class name; 5. Use querySelector() to return the first element object based on the specified selector.

What does javascript use to get dom elements?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

js consists of three parts:

  • ECMAScript: js syntax

  • DOM: Document Object Model

  • BOM: Browser Object Model

WebApi is a standard developed by the W3C organization. In WebApi we mainly Learning DOM and BOM is also a unique part of js for webapi. It is neither a unique category of js itself nor a part of js learning. However, before learning WebApi, you need the foundation of js as the basis of learning WebApi.

DOM

DOM Introduction: Document Object Model (DOM) is a standard programming interface recommended by the w3c group for processing extensible markup language (HTML or XML). w3c has defined a series of DOM interface, through which the content, structure and style of web pages can be changed.

  • Document: A page is a document. Document is used in DOM to represent

  • Element: All tags in the page are elements. In DOM Use element to represent

  • node: All content in a web page can be regarded as a node (labels, attributes, text, comments, etc.). Use node in DOM to represent

Get DOM elements

How to get elements

  • Get based on id, use getElementById () method to get the object with the id element

<div>sj</div>


<script>

// 1、因为页面是从上往下执行的,所以先有标签,后来有script获取
// 2、get 获得 element 元素 by通过 --- 驼峰命名法
// 3、参数 id 是大小写敏感的
// 4、返回的是一个元素
var sj = document.getElementById(&#39;overview&#39;);
console.log(sj)

// dir 打印我们返回的元素对象,更好的查看里面的属性和方法
console.dir(sj)

</script>

What does javascript use to get dom elements?

  • Get based on the name attribute: Use the getElementsByName() method to return the specified The object collection of the name attribute

document.getElementsByName(&#39;name&#39;)
  • Get based on the tag name: Use the getElementsByTagName() method to return a collection of objects with the specified tag name.


        
  • 花海
  •     
  • 晴天
  •     
  • 七里香
  •     
  • 花海
  •     
  • 晴天
        
  • 花海2
  •     
  • 晴天2
  •     
  • 七里香2
  •     
  • 花海2
  •     
  • 晴天2
<script> // 1、返回的是 获取过来元素对象的集合,以伪数组形式存储 var lis = document.getElementsByTagName("li"); console.log(lis); // 获取特定的那个 console.log(lis[2]); // 2、遍历元素对象 for(var i = 0; i < lis.length; i++) { console.log(lis[i]); } // 3、element.getElementByTagName() 可以获取这个元素里面的某些标签 var nav = document.getElementById("nav"); var nav2 = nav.getElementsByTagName(&#39;li&#39;) console.log(nav); </script>

What does javascript use to get dom elements?

Note: Because what we get is a collection of objects, we need to traverse if we want to operate the elements. The element object obtained is dynamic

Acquired through the new method of HTML5

  • getElementsByClassName('Type') Obtains elements according to the class name
// 1、使用 getElementByClassName 获取 class类名元素 
var box = document.getElementsByClassName("box");  // 可以获取全部的 class名为 box的元素
console.log(box);
  • querySelector('selector') Returns the first element object based on the specified selector
// 2、querySelector 返回指定选择器的第一个元素对象 切记:里面选择器需要添加 .box  #nav
var firstBox = document.querySelector('.box')  // 只能获取第一个class名为 box 的元素
console.log(firstBox);

var nav = document.querySelector('#nav')
console.log(nav);

var li = document.querySelector('li')  // 获取的也是第一个 li 元素
console.log(li);
  • querySelectorAll('selector') Returns all the specified selectors Collection of element objects
// 3、querySelectorAll 返回 全部的 li 元素
var allBox = document.querySelectorAll('li') 
console.log(allBox);

Special element acquisition (body, html)

  • Get body element
// 1. 获取 body 元素
var bodyEle = document.body;
console.log(bodyEle)
  • Get html elements
// 2. 获取 HTML 元素
var htmlEle = document.documentElement;
console.log(htmlEle)

Note: The key point is to remember several ways to get ordinary elements. Body and html are not commonly used, just for understanding.

【Related recommendations: javascript video tutorial, Basic programming video

The above is the detailed content of What does javascript use to get dom elements?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:Is javascript safe?Next article:Is javascript safe?