What does javascript use to get dom elements?

青灯夜游
Release: 2022-09-29 16:36:19
Original
5329 people have browsed it

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
sj
Copy after login

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('name')
Copy after login
  • Get based on the tag name: Use the getElementsByTagName() method to return a collection of objects with the specified tag name.

 
  • 花海
  • 晴天
  • 七里香
  • 花海
  • 晴天
Copy after login

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);
Copy after login
  • 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);
Copy after login
  • querySelectorAll('selector') Returns all the specified selectors Collection of element objects
// 3、querySelectorAll 返回 全部的 li 元素 var allBox = document.querySelectorAll('li') console.log(allBox);
Copy after login

Special element acquisition (body, html)

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

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!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!