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

前端 未结 3 1374
难免孤独
难免孤独 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:04

    As you said, HTML id, per specs, must be unique. If one where to put duplicated id, the js behavior relative to those ids will be unpredicatable, and could even change between 2 calls.

    Any js call on one id (jquery or not) will point to one of the id but without guarentee that :

    1. It will be the same every call
    2. It will have the same order between 2 page refresh
    3. It will have the same behavior beween 2 different browser
    4. It will have the same behavior betwween 2 time the same browser

    The problems that could emerge depend on how toghtly the js code is coupled to the underlying element DOM structure anw could mostly point to a undefined exception and stop the js execution.

提交回复
热议问题