Is using the same id multiple times on a page bad practice even when I am not parsing through the page?

后端 未结 4 1700
广开言路
广开言路 2021-01-18 01:36

I understand that using the same id multiple times in a page creates malformed html. When using a jquery selector only the first element with the id will be returned, but in

相关标签:
4条回答
  • 2021-01-18 02:08

    yes it is bad practice. An id should be a unique reference to that element. Use class instead.

    0 讨论(0)
  • 2021-01-18 02:12

    Absolutely. The specs for HTML, CSS, and JavaScript are an agreement as to how a browser will behave when confronted with certain code. Browsers don't make many guarantees when you do anything that's off-spec, and they make even fewer that they would all agree on.

    As Oded mentioned in his comment on the question, browsers build a DOM tree from your code, style it, and render it. So, depending on how a given browser decided to do that, they may run into problems with multiple elements with the same id.

    So, why risk it? I can't think of any instance where a class (or nothing at all) couldn't be used in place of an id.

    0 讨论(0)
  • 2021-01-18 02:21

    You only know what the current requirements are.

    It is best to keep to standards and to valid code/markup as you don't know what will happen in the future.

    In this application, you may very well end up having to use jQuery and then you will be in trouble.

    The solution for styling multiple elements the same way is to use CSS classes - there is absolutely no reason not to do so, as it is just as simple as using ids.

    0 讨论(0)
  • 2021-01-18 02:22

    http://www.w3.org/TR/WCAG20-TECHS/H93.html

    http://www.w3.org/TR/html401/struct/global.html#adef-id

    There are other things that read your HTML document, not just browsers.

    According to the HTML specification, the id attribute MUST be unique on a page (it's not a criteria web designers/developers just invented)

    0 讨论(0)
提交回复
热议问题