Javascript 如何使用 document.getElementByName 和 getElementByTag?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7816863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:42:53  来源:igfitidea点击:

How to use document.getElementByName and getElementByTag?

javascript

提问by Manohar Kulanthai vel

 document.getElementById('frmMain').elements

can i use like this

我可以这样使用吗

document.getElementByName('frmMain').elements 

or

或者

document.getElementBytag('table').elements`

回答by mplungjan

  • document.getElementById('frmMain').elements
    assumes the form has an ID and that the ID is unique as IDs should be. Although it also accesses a nameattribute in IE, please add ID to the element if you want to use getElementById






A great alternative is

一个很好的选择是



In all of the above, the .elementscan be replaced by for example .querySelectorAll("[type=text]")to get all text elements

在上面的所有内容中,.elements可以替换为例如.querySelectorAll("[type=text]")获取所有文本元素

回答by RobG

getElementByIdreturns either a reference to an element with an id matching the argument, or null if no such element exists in the document.

getElementById返回对具有与参数匹配的 id 的元素的引用,如果文档中不存在这样的元素,则返回 null。

getElementsByName()(note the plural Elements) returns a (possibly empty) HTMLCollection of the elements with a name matching the argument. Note that IE treats the nameand idattributes and properties as the same thing, so getElementsByNamewill return elements with matching idalso.

getElementsByName()(注意复数Elements)返回名称与参数匹配的元素的(可能为空)HTMLCollection。请注意,IE 将nameid属性和属性视为同一件事,因此getElementsByName也将返回具有匹配id 的元素。

getElementsByTagNameis similar but returns a NodeList. It's all there in the relevant specifications.

getElementsByTagName类似,但返回一个NodeList。这一切都在相关规范中。

回答by nnnnnn

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".

getElementsByName()getElementsByTagName()- 注意“Elements”中的“s”,表示这两个函数都返回一个元素列表,即一个 NodeList,您将像访问数组一样访问它。请注意,第二个函数以“TagName”而不是“Tag”结尾。

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

即使该函数只返回一个元素,它仍将位于长度为 1 的 NodeList 中。所以:

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

回答by alex

I assume you are talking about getElementById()returning a reference to an element whilst the others return a node list. Just subscript the nodelist for the others, e.g. document.getElementBytag('table')[4].

我假设您正在谈论getElementById()返回对元素的引用,而其他人则返回节点列表。只需为其他节点列表添加下标,例如document.getElementBytag('table')[4].

Also, elementsis only a property of a form (HTMLFormElement), not a tablesuch as in your example.

此外,elements只是形式 ( HTMLFormElement)的属性,而不是table您的示例中的属性。

回答by Sai Kalyan Kumar Akshinthala

If you have given same text name for both of your Id and Name properties you can give like document.getElementByName('frmMain')[index]other wise object required error will come.And if you have only one table in your page you can use document.getElementBytag('table')[index].

如果您为 Id 和 Name 属性提供了相同的文本名称,您可以提供document.getElementByName('frmMain')[index]其他明智的对象所需的错误。如果您的页面中只有一个表格,您可以使用document.getElementBytag('table')[index].

EDIT:

编辑:

You can replace the index according to your form, if its first form place 0 for index.

您可以根据您的表单替换索引,如果它的第一个表单为索引放置 0。

回答by bitsmonkey

  1. The getElementsByName()method accesses all elements with the specified name. this method returns collection of elements that is an array.
  2. The getElementsByTagName()method accesses all elements with the specified tagname. this method returns collection of elements that is an array.
  3. Accesses the first element with the specified id. this method returns only a single element.
  1. getElementsByName()方法访问具有指定名称的所有元素。此方法返回作为数组的元素集合。
  2. getElementsByTagName()方法访问具有指定标记名的所有元素。此方法返回作为数组的元素集合。
  3. 访问具有指定 id 的第一个元素。此方法仅返回单个元素。

eg:

例如:

<script type="text/javascript">
    function getElements() {
        var x=document.getElementById("y");
??      alert(x.value);
    }
</script>
</head>
<body>
    <input name="x" id="y" type="text" size="20" /><br />

This will return a single HTML element and display the value attribute of it.

这将返回一个 HTML 元素并显示它的 value 属性。

<script type="text/javascript">
    function getElements() {
??      var x=document.getElementsByName("x");
??      alert(x.length);
??  }
</script>
</head>
<body>
    <input name="x" id="y" type="text" size="20" /><br />
    <input name="x" id="y" type="text" size="20" /><br />

this will return an array of HTML elements and number of elements that match the name attribute.

这将返回一个 HTML 元素数组和与 name 属性匹配的元素数量。

Extracted from w3schools.

摘自 w3schools。