What practical problem will i face if i use same #id more than one time on a page , other than validation error “ID is already defined”?

前端 未结 7 806
甜味超标
甜味超标 2021-01-13 15:09

First of all I alwyas use one #ID. just asking this question to know deep reason behind it.

Is it only a matter of W3C validation? or more than that

7条回答
  •  深忆病人
    2021-01-13 15:39

    The result is undefined.

    Apart from what the rest said about getElementById:

    • using #foo in CSS may apply to all elements with id="foo", or to the first, or to the last, depending on how the browser is implemented.
    • using page.html#foo in a URL may scroll to one of the elements, or to the other, or to neither.

    edit: either way, you should teach your students to do things right even if doing it wrong doesn't seem to have any immediate bad consequences... ;)

提交回复
热议问题