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

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:

主站蜘蛛池模板: 吉木乃县| 苍溪县| 吴江市| 商丘市| 宜章县| 巴林右旗| 平江县| 家居| 高邑县| 柳河县| 深水埗区| 通道| 巧家县| 宣恩县| 宣城市| 酉阳| 广水市| 瓦房店市| 怀宁县| 高雄市| 昌邑市| 周至县| 宁德市| 墨竹工卡县| 嘉禾县| 易门县| 杭锦旗| 京山县| 宁武县| 尉氏县| 绿春县| 武川县| 大石桥市| 凤城市| 铜鼓县| 安义县| 溆浦县| 莱州市| 南宫市| 门源| 赞皇县|