This commit is contained in:
Ilya Kantor 2019-05-25 17:29:40 +03:00
parent 09db684a7d
commit f0affb745d
5 changed files with 30 additions and 30 deletions

View file

@ -35,7 +35,7 @@ If we declare a variable with the same name, it takes precedence:
<script>
let elem = 5;
alert(elem); // the variable overrides the element
alert(elem); // 5
</script>
```
@ -172,7 +172,7 @@ Today, they are mostly history, as `querySelector` is more powerful and shorter
So here we cover them mainly for completeness, while you can still find them in the old scripts.
- `elem.getElementsByTagName(tag)` looks for elements with the given tag and returns the collection of them. The `tag` parameter can also be a star `"*"` for "any tags".
- `elem.getElementsByClassName(className)` returns elements that have the given CSS class. Elements may have other classes too.
- `elem.getElementsByClassName(className)` returns elements that have the given CSS class.
- `document.getElementsByName(name)` returns elements with the given `name` attribute, document-wide. very rarely used.
For instance:
@ -305,8 +305,6 @@ If we use it instead, then both scripts output `1`:
Now we can easily see the difference. The static collection did not increase after the appearance of a new `div` in the document.
Here we used separate scripts to illustrate how the element addition affects the collection, but any DOM manipulations affect them. Soon we'll see more of them.
## Summary
There are 6 main methods to search for nodes in DOM:
@ -367,5 +365,5 @@ Besides that:
- There is `elem.matches(css)` to check if `elem` matches the given CSS selector.
- There is `elem.closest(css)` to look for the nearest ancestor that matches the given CSS-selector. The `elem` itself is also checked.
And let's mention one more method here to check for the child-parent relationship:
And let's mention one more method here to check for the child-parent relationship, as it's sometimes useful:
- `elemA.contains(elemB)` returns true if `elemB` is inside `elemA` (a descendant of `elemA`) or when `elemA==elemB`.

View file

@ -186,7 +186,7 @@ In the example above:
- Changing the attribute `value` updates the property.
- But the property change does not affect the attribute.
That "feature" may actually come in handy, because the user may modify `value`, and then after it, if we want to recover the "original" value from HTML, it's in the attribute.
That "feature" may actually come in handy, because the user actions may lead to `value` changes, and then after them, if we want to recover the "original" value from HTML, it's in the attribute.
## DOM properties are typed
@ -216,9 +216,9 @@ There are other examples. The `style` attribute is a string, but the `style` pro
</script>
```
That's an important difference. But even if a DOM property type is a string, it may differ from the attribute!
Most properties are strings though.
For instance, the `href` DOM property is always a *full* URL, even if the attribute contains a relative URL or just a `#hash`.
Quite rarely, even if a DOM property type is a string, it may differ from the attribute. For instance, the `href` DOM property is always a *full* URL, even if the attribute contains a relative URL or just a `#hash`.
Here's an example:
@ -380,7 +380,7 @@ Methods to work with attributes are:
- `elem.removeAttribute(name)` -- to remove the attribute.
- `elem.attributes` is a collection of all attributes.
For most needs, DOM properties can serve us well. We should refer to attributes only when DOM properties do not suit us, when we need exactly attributes, for instance:
For most situations using DOM properties is preferable. We should refer to attributes only when DOM properties do not suit us, when we need exactly attributes, for instance:
- We need a non-standard attribute. But if it starts with `data-`, then we should use `dataset`.
- We want to read the value "as written" in HTML. The value of the DOM property may be different, for instance the `href` property is always a full URL, and we may want to get the "original" value.