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
How to use document.getElementByName and getElementByTag?
提问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 aname
attribute in IE, please add ID to the element if you want to use getElementById
- document.getElementById('frmMain').elements
假设表单有一个 ID,并且 ID 应该是唯一的。虽然它也在name
IE中访问了一个属性,但如果你想使用getElementBy Id,请在元素中添加ID
- document.getElementsByName('frmMain')[0].elements
will get the elements of the first object named frmMain on the page - notice the plural getElements- it will return a collection.
- document.getElementsByName('frmMain')[0].elements
将获取页面上名为 frmMain 的第一个对象的元素 - 注意复数 getElement s- 它会返回一个集合。
- document.getElementsByTagName('form')[0].elements
will get the elements of the first form on the page based on the tag - again notice the plural getElements
- document.getElementsByTagName('form')[0].elements
将根据标签获取页面上第一个表单的元素 - 再次注意复数 getElement s
A great alternative is
一个很好的选择是
- document.querySelector("form").elements
will get the elements of the first form on the page. The "form" is a valid CSS selector
- document.querySelector("form").elements
将获取页面上第一个表单的元素。“表单”是一个有效的 CSS 选择器
- document.querySelectorAll("form")[0].elements
notice theAll
- it is a collection. The [0] will get the elements of the first form on the page. The "form" is a valid CSS selector
- document.querySelectorAll("form")[0].elements
注意All
- 它是一个集合。[0] 将获取页面上第一个表单的元素。“表单”是一个有效的 CSS 选择器
In all of the above, the .elements
can 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 将name和id属性和属性视为同一件事,因此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, elements
is only a property of a form (HTMLFormElement
), not a table
such 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
- The
getElementsByName()
method accesses all elements with the specified name. this method returns collection of elements that is an array. - The
getElementsByTagName()
method accesses all elements with the specified tagname. this method returns collection of elements that is an array. - Accesses the first element with the specified id. this method returns only a single element.
- 该
getElementsByName()
方法访问具有指定名称的所有元素。此方法返回作为数组的元素集合。 - 该
getElementsByTagName()
方法访问具有指定标记名的所有元素。此方法返回作为数组的元素集合。 - 访问具有指定 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。