官术网_书友最值得收藏!

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&gt;
<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:

主站蜘蛛池模板: 古交市| 工布江达县| 镇雄县| 织金县| 思茅市| 隆昌县| 天全县| 开远市| 元朗区| 农安县| 图木舒克市| 固始县| 白城市| 和硕县| 陇川县| 岱山县| 大庆市| 淮南市| 清水县| 永安市| 静宁县| 枝江市| 乌什县| 新建县| 阿克陶县| 开封县| 布尔津县| 左权县| 若尔盖县| 青铜峡市| 通化县| 修水县| 东海县| 马尔康县| 山东省| 长岛县| 五指山市| 米脂县| 新竹县| 大丰市| 沽源县|