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

In addition to using the id selector, can the css style uniquely determine a dom element_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 12:15:251550browse

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?


Reply to the discussion (solution)

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:


....
  • ...< ;/li>


Suppose you want to find the LI inside, there is no need to traverse all the element objects in the document, just go through the superior DIV where its superior UL is located , because it has an ID named theforever_csdn, just find it and then find the LI sub-object in its UL sub-object.

This is to find child elements through parent elements. Similarly, you can also find sibling elements through sibling elements, and find parent elements through child elements. Just look at the DOM methods more.

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:


....
  • ...< ;/li>


Suppose you want to find the LI inside, there is no need to traverse all the element objects in the document, just go through the superior DIV where its superior UL is located , because it has an ID named thefore...
Does anyone know how to uniquely determine a DOM element besides the ID? There is no way through class and parent dom. Or maybe I didn't expect it.

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn