Home >Web Front-end >HTML Tutorial >In addition to using the id selector, can the css style uniquely determine a dom element_html/css_WEB-ITnose
The function we are working on needs to determine the target of modification. However, not every DOM element has an ID, so we are stuck here. How to determine a DOM element when there is no ID?
There are many ways to choose based on conditions, such as through class attributes,
For example, traversing to determine other attributes
The original poster Just look at JQ's selector usage and you will know that there are many forms. In fact, in essence, its principle is just like the one mentioned above, which is to traverse the element and judge based on its other attributes, but it is much more convenient than writing it yourself.
Of course, it is not recommended to use JQ just because it is easier to obtain a few objects. If you have time, take a closer look at its functions. If you can let it play more roles in your page, the ratio of convenience and cost of using it will be more appropriate.
I forgot to mention one more useful thing, which is to find your target through other easy-to-find targets related to the one you are looking for. Haha, it sounds convoluted, but it is actually easy to understand, such as:
I forgot to mention one more useful thing, which is to find your target through other easy-to-find targets related to the one you are looking for. Haha, it sounds convoluted, but it is actually easy to understand, such as:
Hey, it’s frustrating. There is no way to uniquely determine a DOM element. Through class and superior-subordinate relationships, there is no guarantee that it is definitely unique
Hey, it’s frustrating. There is no way to uniquely determine a DOM element. Elements, through class and superior-subordinate relationships, are not guaranteed to be unique
If CSS3 is supported:
section:nth-child(1) {} /* Select the first
section:nth-child(2) {} /* Select the second
section:nth-child(N) {} /* Select the Nth
section:nth-last-child(1) {} /* Select the last one
section:nth-last-child(2) {} /* Select the second to last one < ;section> */
section:nth-last-child(N) {} /* Select the Nth from the bottom
The child elements can be positioned one by one, how come? Can't guarantee uniqueness? This is more accurate than using ID. Because you can actually write the same ID for several times, of course, the result will only find the first one, but if you forget to write it before, it is not impossible to write the same ID later.
It’s more complicated. You need to determine the position of the dom element in the dom document tree
and this position is already determined at the
mark
You can completely know a certain The dom element is the number of the dom element with the same name, and the serial number can be uniquely positioned
Of course, any addition or deletion of the html may cause the serial number to change
Because positioning is not very convenient , that’s why the id selector appeared
So when planning the page, setting some ids appropriately is beneficial to later work
ID, CLASS, or node subscript, all are fine