The DOM (Video Version)
-
0:00 - 0:07Going forward, you're going to hear
me say a certain acronym a lot: DOM. -
0:07 - 0:10Dom de dom dom dom
-
0:10 - 0:16DOM stands for
Document Object Model, -
0:16 - 0:19and it's the way that browsers
let us developers -
0:19 - 0:22manipulate webpages with JavaScript.
-
0:22 - 0:27Whenever a browser loads a webpage,
it parses all the HTML and CSS, -
0:27 - 0:30and converts the document into a DOM.
-
0:30 - 0:34That DOM is really a
big JavaScript object -
0:34 - 0:38that contains everything we'd ever
want to know or change about a page, -
0:38 - 0:43like every single tag, and
attribute and style for each tag. -
0:43 - 0:47To access the DOM on a
webpage from JavaScript, -
0:47 - 0:53we use the global variable `document`,
and then we can use properties -
0:53 - 0:58and call functions that
are attached to this object here. -
0:58 - 1:04The general strategy for
DOM manipulation is two steps. -
1:04 - 1:09Let me just make a list of them here.
-
1:09 - 1:15Here we go, and then we
have the second step. -
1:15 - 1:18Let's go through each
of these steps now. -
1:18 - 1:23So the first step is to find the DOM node
using an access method. -
1:23 - 1:29If all we're looking for is the
tag, we can access that really easily -
1:29 - 1:34just by saying `document.body`.
-
1:34 - 1:39And now the second step is to
manipulate the DOM node that we found -
1:39 - 1:43by changing its attributes,
styles, inner HTML, -
1:43 - 1:46appending new nodes to it, ... .
-
1:46 - 1:50So if we just want to replace
the contents of the whole tag, -
1:50 - 1:54then we can use the
`innerHTML` property. -
1:54 - 1:59So `document.body.innerHTML =
"Webpage be gone!";` -
1:59 - 2:02Ta da, I did it.
-
2:02 - 2:06The browser is watching for
changes to the document object, -
2:06 - 2:10and as soon as it sees you change the
innerHTML of document.body, -
2:10 - 2:13it reflects it back in
the actual document. -
2:13 - 2:18Remember, the document object
isn't the actual webpage, -
2:18 - 2:24but the browser tries to make it reflect
the current page as much as possible. -
2:24 - 2:29Now, there are a lot more
ways to do step one, -
2:29 - 2:33because usually you want to find
something besides just the body tag. -
2:33 - 2:38Maybe you want to find a tag with a
certain ID, or all tags of a certain type-- -
2:38 - 2:42That's what we'll talk about in
the DOM access methods tutorial. -
2:42 - 2:45There's also a lot more cool stuff
you can do in step two, -
2:45 - 2:49like changing the attributes
or styles of the tags you find. -
2:49 - 2:53We'll talk about all that in the
DOM modification tutorial. -
2:53 - 2:58Once you've mastered DOM access and
manipulation, we'll move on to fun ways -
2:58 - 3:03to use it, like when responding to
user events or making an animation. -
3:03 - 3:07Right now, it is a little silly
that we use JavaScript to do -
3:07 - 3:10what we could have just done
with HTML at the beginning, -
3:10 - 3:15but trust me, you'll want to master
DOM access and DOM modification -
3:15 - 3:19so that you can make full
interactive experiences later. -
3:19 - 3:25So, keep it up, and see you soon.
- Title:
- The DOM (Video Version)
- Description:
-
This is a video recording of a talk-through, uploaded to make it easier to create subtitles.
Please watch the original interactive talk-through on Khan Academy, where you can pause and edit the code
and see the code in better resolution:
http://www.khanacademy.org/computer-programming - Video Language:
- English
- Duration:
- 03:26
Janice Holz edited English subtitles for The DOM (Video Version) | ||
Janice Holz edited English subtitles for The DOM (Video Version) |