Why can only forms be referenced by dot notation?

后端 未结 2 367
萌比男神i
萌比男神i 2021-01-13 18:19

It seems like a simple beginners question, but I\'m unable to find an answer anywhere.

Let\'s say I have this HTML:

&
相关标签:
2条回答
  • 2021-01-13 18:34

    The syntax you've posted is for form, to be specific.. It will work only with forms..

    You will need to use following line to make it work for divs...

    document.getElementsByName("myDiv")[0].getElementsByTagName("span")[0].innerHTML = "bar";
    

    There are other ways to do this too...

    0 讨论(0)
  • 2021-01-13 18:53

    As designed by W3C.

    There's really nothing to argue about. You can access the an HTMLCollection of all the forms on the page through document.forms, much like you can images with document.images, applets with document.applets, links with document.links and anchors with document.anchors.

    http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-1689064

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