Can multiple different HTML elements have the same ID if they're different elements?

后端 未结 16 1843
无人及你
无人及你 2020-11-21 05:30

Can multiple HTML elements have the same ID if they\'re of different element types? Is a scenario like this valid? Eg:

div#foo
span#foo
a#foo
16条回答
  •  北海茫月
    2020-11-21 06:04

    And for what it's worth, on Chrome 26.0.1410.65, Firefox 19.0.2, and Safari 6.0.3 at least, if you have multiple elements with the same ID, jquery selectors (at least) will return the first element with that ID.

    e.g.

    first text for one
    second text for one

    and

    alert($('#one').size());
    

    See http://jsfiddle.net/RuysX/ for a test.

提交回复
热议问题