- Vue.js 2 Design Patterns and Best Practices
- Paul Halliday
- 196字
- 2021-06-24 18:33:11
DOM
The DOM is what is used to describe the structure of an HTML or XML page. It creates a tree-like structure that provides us with the ability to do everything from creating, reading, updating, and deleting nodes to traversing the tree and many more features, all within JavaScript. Let's consider the following HTML page:
<!DOCTYPE html>
<html lang="en">
<head>
<title>DOM Example</title>
</head>
<body>
<div>
<p>I love JavaScript!</p>
<p>Here's a list of my favourite frameworks:</p>
<ul>
<li>Vue.js</li>
<li>Angular</li>
<li>React</li>
</ul>
</div>
<script src="app.js"></script>
</body>
</html>
We're able to look at the HTML and see that we have one div, two p, one ul, and li tags. The browser parses this HTML and produces the DOM Tree, which at a high level looks similar to this:

We can then interact with the DOM to get access to these elements by TagName using document.getElementsByTagName(), returning a HTML collection. If we wanted to map over these collection objects, we could create an array of these elements using Array.from. The following is an example:
const paragraphs = Array.from(document.getElementsByTagName('p'));
const listItems = Array.from(document.getElementsByTagName('li'));
paragraphs.map(p => console.log(p.innerHTML));
listItems.map(li => console.log(li.innerHTML));
This should then log the innerHTML of each item to the console inside of our array(s), thus showing how we can access items inside of the DOM:

- EDA技術與VHDL編程
- 物聯網工程規劃技術
- 物聯網短距離無線通信技術應用與開發
- Hands-On Full Stack Development with Spring Boot 2 and React(Second Edition)
- 區塊鏈輕松上手:原理、源碼、搭建與應用
- Echo Quick Start Guide
- 無線傳感器網絡定位技術
- 大型企業微服務架構實踐與運營
- 移動物聯網:商業模式+案例分析+應用實戰
- 一本書讀懂TCP/IP
- 園區網絡架構與技術
- 現場綜合化網絡運營與維護:運營商數字化轉型技術與實踐
- 新IP:面向泛在全場景的未來數據網絡
- OSPF協議原理與功能拓展
- Mastering Prezi for Business Presentations