what happens if one set id attribute values of several html tags, the same string?

前端 未结 3 1404
难免孤独
难免孤独 2021-01-29 09:48

I recently saw a html page, that i thought the id of several html tags was the same, then I realized the ids are unique but it raises the question that what could have happened

3条回答
  •  梦如初夏
    2021-01-29 10:00

    Duplicate ids can have various different effects. Which you experience will depend on the method you use to try to access them (and possibly also from browser to browser).

    • You'll affect all of them
    • You'll affect the first one
    • You'll affect the last one
    • You'll get a collection instead of an element, try to treat it like an element and get an error

    Duplicate ids are not allowed in HTML. Don't make trouble for yourself. Use classes for groups and ids for unique identifiers.

提交回复
热议问题