Why should one add ID to their HTML tags?

后端 未结 9 2041
日久生厌
日久生厌 2020-11-29 14:16

A simple question: why should we add the id into our HTML tags if they work perfectly well without them? I know that one of their uses is being able to navigate

相关标签:
9条回答
  • 2020-11-29 14:43

    it is there to help you identify your element in java-script code.the getElementByID function in java-script give the handle of an element with specific ID for you.like this.

    var someElement = document.getelementById("someID");
    // do whatever with someElement;
    
    0 讨论(0)
  • 2020-11-29 14:47

    I myself also prefer class for styling through CSS but sometimes you need an element to be unique. For accessibility reasons you use id to input elements to "connect" its label to it by using for attribute. And for Javascript it's much simpler to select an element if it has got id attribute.

    0 讨论(0)
  • 2020-11-29 14:50

    Uses of id attributes in HTML

    • As a target for a fragment identifier on a URL.
    • As a target on form controls for the for attribute on <label> and <output> elements.
    • As a target on <form> elements for the form attribute on form associated elements.
    • As a target for element references via the microdata itemref attribute.
    • As a target for element references via some ARIA attributes including aria-describedby, aria-labelledby and 4 others.
    • As a target on <th> elements for the headers attribute on <td> and <th> elements.
    • As a target on <menu> elements for the contextmenu attribute.
    • As a target on <datalist> elements for the list attribute on <input> elements.
    • As part of a hash-name reference to <map> elements for the usemap attribute on the <img> and <object> elements.
    • As an identifier of an element in a CSS selector
    • As an identifier of an element for JavaScript processing
    0 讨论(0)
提交回复
热议问题