Can an html element have multiple ids?

前端 未结 18 2345
天命终不由人
天命终不由人 2020-11-22 07:40

I understand that an id must be unique within an HTML/XHTML page.

My question is, for a given element, can I assign multiple ids to it?

相关标签:
18条回答
  • 2020-11-22 07:58
    Any ID assigned to a div element is unique.
    However, you can assign multiple IDs "under", and not "to" a div element.
    In that case, you have to represent those IDs as <span></span> IDs.
    
    Say, you want two links in the same HTML page to point to the same div element in  the page.
    
    The two different links
    <p><a href="#exponentialEquationsCalculator">Exponential Equations</a></p>         
    
    <p><a href="#logarithmicExpressionsCalculator"><Logarithmic Expressions</a></p>
    
    Point to the same section of the page. 
    <!-- Exponential / Logarithmic Equations Calculator -->
    <div class="w3-container w3-card white w3-margin-bottom">      
       <span id="exponentialEquationsCalculator"></span>
       <span id="logarithmicEquationsCalculator"></span>
    </div>
    
    0 讨论(0)
  • 2020-11-22 07:58

    Nay.

    https://www.w3.org/TR/2011/WD-html5-20110525/elements.html#the-id-attribute

    The value must not contain any space characters.

    id="a b" <-- find the space character in that VaLuE.

    That said, you can style multiple IDs. But if you're following spec, the answer is no.

    0 讨论(0)
  • 2020-11-22 07:59

    No, you should use nested DIVs if you want to head down that path. Besides, even if you could, imagine the confusion it would cause when you run document.getElementByID(). What ID is it going to grab if there are multiple ones?

    On a slightly related topic, you can add multiple classes to a DIV. See Eric Myers discussion at,

    http://meyerweb.com/eric/articles/webrev/199802a.html

    0 讨论(0)
  • 2020-11-22 07:59

    I don´t think you can have two Id´s but it should be possible. Using the same id twice is a different case... like two people using the same passport. However one person could have multiple passports... Came looking for this since I have a situation where a single employee can have several functions. Say "sysadm" and "team coordinator" having the id="sysadm teamcoordinator" would let me reference them from other pages so that employees.html#sysadm and employees.html#teamcoordinator would lead to the same place... One day somebody else might take over the team coordinator function while the sysadm remains the sysadm... then I only have to change the ids on the employees.html page ... but like I said - it doesn´t work :(

    0 讨论(0)
  • 2020-11-22 08:00

    No. From the XHTML 1.0 Spec

    In XML, fragment identifiers are of type ID, and there can only be a single attribute of type ID per element. Therefore, in XHTML 1.0 the id attribute is defined to be of type ID. In order to ensure that XHTML 1.0 documents are well-structured XML documents, XHTML 1.0 documents MUST use the id attribute when defining fragment identifiers on the elements listed above. See the HTML Compatibility Guidelines for information on ensuring such anchors are backward compatible when serving XHTML documents as media type text/html.

    0 讨论(0)
  • 2020-11-22 08:02

    I'd like to say technically yes, since really what gets rendered is technically always browser dependent. Most browsers try to keep to the specifications as best they can and as far as I know there is nothing in the css specifications against it. I'm only going to vouch for the actual html,css,javascript code that gets sent to the browser before any other interpretter steps in.

    However I also say no since every browser I typically test on doesn't actually let you. If you need to see for yourself save the following as a .html file and open it up in the major browsers. In all browsers I tested on the javascript function will not match to an element. However, remove either "hunkojunk" from the id tag and all works fine. Sample Code

    <html>
    <head>
    </head>
    <body>
        <p id="hunkojunk1 hunkojunk2"></p>
    
    <script type="text/javascript">
        document.getElementById('hunkojunk2').innerHTML = "JUNK JUNK JUNK JUNK JUNK JUNK";
    </script>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题