behavior of javascript getElementById() when there are elements with duplicate IDs in HTML DOM?

后端 未结 5 1319
感动是毒
感动是毒 2020-12-04 01:42

Can someone shed some light on the behavior of javascript getElementById() when there are elements with duplicate IDs in HTML DOM??

5条回答
  •  Happy的楠姐
    2020-12-04 02:23

    yes.It will return to the first one.We can not use same ID as a HTML attributes
    1. * view plaincopy to clipboardprint?

    * {  
     margin: 0;  
     padding: 0;  
    }     
    

    Let’s knock the obvious ones out, for the beginners, before we move onto the more advanced selectors.

    The star symbol will target every single element on the page. Many developers will use this trick to zero out the margins and padding. While this is certainly fine for quick tests, I’d advise you to never use this in production code. It adds too much weight on the browser, and is unnecessary.

    The * can also be used with child selectors. view plaincopy to clipboardprint?

    #container * {  
     border: 1px solid black;  
    }     
    

    This will target every single element that is a child of the #container div. Again, try not to use this technique very much, if ever.

    X

    view plaincopy to clipboardprint?

    #container {  
       width: 960px;  
       margin: auto;  
    }     
    

    Prefixing the hash symbol to a selector allows us to target by id. This is easily the most common usage, however be cautious when using id selectors.

提交回复
热议问题