简体   繁体   English

哪些标签在form.elements中计数?

[英]Which tags are counted in form.elements?

What tag is counted in form.elements ? form.elements计入了什么标签?

<html>
   <head>
      <title>
          form process
      </title>
      <script>
          function showelements()
          {
              var form = document.getElementById("f");
              var n = form.elements.length;
              alert(n);
          }
      </script>
   </head>
   <body>
         <form id="f" action="#" method="post">
            <p>Hello</p>
            <input type="text" />
            <input type="text" />
            <button onclick="showelements()">Click</button>
        </form>
   </body>
</html>

I think n = 4 because there are 4 elements: p , input , input , button , but the result n = 3 . 我认为n = 4是因为有4个元素: pinputinputbutton ,但是结果n = 3

The elements collection contains only form controls (input, select, button, textarea and fieldset). elements集合仅包含表单控件 (输入,选择,按钮,文本区域和字段集)。

If you want all the elements in the form, use frm.getElementsByTagName("*") . 如果要使用表单中的所有元素,请使用frm.getElementsByTagName("*")

Ok this time from W3C: http://www.w3.org/TR/html5/forms.html#dom-form-elements 好的,这次是从W3C来的: http : //www.w3.org/TR/html5/forms.html#dom-form-elements

The elements IDL attribute must return an HTMLFormControlsCollection [...] elements IDL属性必须返回HTMLFormControlsCollection [...]

Here is a more comprehensive list : 这是一个更全面的列表

  • button
  • fieldset
  • input
  • keygen
  • object
  • output
  • select
  • textarea

"why <input type="image" src="" /> is not counted?" “为什么不计算<input type="image" src="" />

Because, historical reasons... 因为历史原因...

[...] with the exception of input elements whose type attribute is in the Image Button state, which must, for historical reasons, be excluded from this particular collection [HTMLFormControlsCollection]. 除了其type属性处于“ 图像按钮”状态的input元素外,出于历史原因,这些元素必须从此特定集合[HTMLFormControlsCollection]中排除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM