Do you know how to control every page element? Here we will give you a brief introduction to the recognition control of page elements in DHTML and JS. I believe you will be interested in it.
Identification Control of page elements in DHTML and JS
In the process of creating a dynamic web page, we often need to perform operations on the corresponding page elements, which requires us to be able to accurately control each page element.
Generally, elements in a webpage can be identified in multiple ways, such as context-based identification or element recognition.
There are three most common element identification methods in a webpage: ID, CLASS, and NAME. They are represented to a single element identity. CLASS is used to NAME the same type, while NAME is used to NAME a general element.
For example, you can name it as follows:
- <imgid="pic"src="..">
- <spanname="kuai"></span>
- <divclass="layer"></div>
When we call these elements in JAVASCRIPT, we can use document. all. pic, document. all. kuai to identify them.
Generally, a name is only used for the name of an element, but it is not exactly the same. Sometimes elements of the same type must have the same effect, not only can they be identified by CLASS, you can also use the ID or NAME to identify all the identical IDs and names.
However, this poses a problem: how to identify an element.
In this case, the difference between recognition and single name recognition lies in the addition of digital indexes. For example, the previous pic can also use document. all. pic [1] is used for identification, and the others are identified by numbers in the order they appear on the page.
In many cases, the control function is triggered by the corresponding event, so we may use the element that triggers the event. In this case, we can use eventElement to identify the event, it indicates the element that triggers the event.
At the same time, parentElement and childrenElement can also be used to identify the upper-level and lower-level elements that trigger event elements.
When eventElement is used to obtain a trigger element, we can also obtain its identifier name. For example, if eventElement. tagName is used to obtain the HTML Identification name of the element, eventElement. id is used to obtain the name of the element, and eventElement. name is used to obtain the name of the element.