behavior of javascript getElementById() when there are elements with duplicate IDs in HTML DOM?

后端 未结 5 1317
感动是毒
感动是毒 2020-12-04 01:42

Can someone shed some light on the behavior of javascript getElementById() when there are elements with duplicate IDs in HTML DOM??

5条回答
  •  萌比男神i
    2020-12-04 02:24

    Yes, the behaviour is undefined.

    The markup is invalid, and there is no standard that defines what the browser should do in that situation.

    Each browser will try to do something reasonable, usually return the first element. Other possible ways that it could be handled would be to return the last element, no element at all (null), or throw an error.

提交回复
热议问题