id style attribute
<html> <head> <style> #id320 { color: red; } </style> </head> <body> <input id="id320" value="0" /> <input id="id320" value="9" /> <button onclick="document.getElementById('id320').value='1'">button</button> </body></html>
It is generally best not to repeat id
Light Nothing can be seen in this part
What is confusing is
input id="id320"
Is the id here setting the style or defining the logo?
It is generally best not to repeat the id
You can’t tell much from this part alone
What’s confusing is
input id="id320"
What is the id here? Is it setting a style or defining a logo?
You have to look at the context and actual performance of the code to understand the meaning of the code
Of course, sometimes the default and standardized writing methods are not used and will make other readers confused
Just like the code you posted
id is theoretically the only one that represents the whole world
But now there are 2 identical ones hehe
so we can only look at the end of the code Let’s understand the meaning through the performance
If ID is not used in JS, there is no problem in writing like this. Like class, if ID is used in JS, writing in this way will definitely cause problems. Therefore, it is best not to have duplicate IDs.
Can this be understood?
Specification requirements:
ID has only one meaning, which is to uniquely identify an element
The id value must be unique
An id style is only defined for a unique element
When it is not standardized (multiple elements have the same id), the general approach (such as IE/Chrome/Firefox):
getElementById returns the first element found
Set styles for elements with the same id
The id cannot be repeated, it is not a matter of "it's better not to"
It's okay to write duplicates. That's because of the browser's fault tolerance (this is also One of the important reasons why browser vendors collectively boycott xhtml)
But it is wrong to repeat ids grammatically