How to use document.getElementByName and getElementByTag?

前端 未结 6 1759
一生所求
一生所求 2020-11-29 08:38
 document.getElementById(\'frmMain\').elements

can i use like this

document.getElementByName(\'frmMain\').elements 
6条回答
  •  悲&欢浪女
    2020-11-29 08:59

    It's getElementsByName() and getElementsByTagName() - note the "s" in "Elements", indicating that both functions return a list of elements, i.e., a NodeList, which you will access like an array. Note that the second function ends with "TagName" not "Tag".

    Even if the function only returns one element it will still be in a NodeList of length one. So:

    var els = document.getElementsByName('frmMain');
    // els.length will be the number of elements returned
    // els[0] will be the first element returned
    // els[1] the second, etc.
    

    Assuming your form is the first (or only) form on the page you can do this:

    document.getElementsByName('frmMain')[0].elements
    document.getElementsByTagName('table')[0].elements
    

提交回复
热议问题