我不知道它叫什么,但我知道有一种方法可以根据元素的标签获取元素,而无需 getElementsByTagName
。它返回相同的内容,但它更短,而且我在我的项目中经常使用标签。我所说的是 document.frames[x]
和 document.images[x]
,但还有其他元素,例如 document.b[x]
或document.a[x]
。由于 document.images
与
标签不同,似乎如果有更多标签,它们的命名也会有所不同。使用此方法时是否有人知道它的名称和/或有可接受的标签列表?谢谢。
PS 请不要建议使用 jQuery 等库。该项目旨在成为一种学习体验,因此我想使用常规 JavaScript。
I don't know what it's called, but I know that there's a way to get elements based on their tags without getElementsByTagName
. It returns the same thing, but it's shorter and I'm using tags a lot in my project. What I'm talking about is document.frames[x]
and document.images[x]
, but with other elements, like document.b[x]
or document.a[x]
. Seeing as document.images
isn't the same as the <img>
tag, it seems like if there are more they'd be named differently as well. Would anyone happen to know what it's called when using this method and/or have a list of accepted tags? Thanks.
P.S. Please do not suggest using a library such as jQuery. This project is meant to be a learning experience, so I want to use regular JavaScript.
发布评论
评论(3)
正如答案中其他地方提到的,这实际上与 JavaScript 没有任何关系,这些是可通过 DOM 的 JavaScript 语言绑定。
参考诸如
document.frames[x]
之类的寻址元素(请注意,这是不正确的,它应该是window.frames[x]
) 和document.images[x]
- 这些是 <一个href="http://www.w3.org/TR/REC-DOM-Level-1/level-one-html.html#ID-26809268" rel="noreferrer">文档对象/HTML 集合 W3C 标准仅包括图像、小程序、链接、表单
和锚点
。因此,除非我正在寻找完全错误的地方,否则从 DOM-1 和 DOM-2 规范,似乎没有任何方法可以像您记得的那样通过标签名称任意寻址元素。
更新
HTMLCollection
上的 MDC 条目 更容易理解;它读着As mentioned elsewhere in the answers, this doesn't have anything to do with JavaScript really, these are DOM properties and methods accessible via the JavaScript language binding for the DOM.
With reference to addressing elements such as
document.frames[x]
(note that this is incorrect, it should bewindow.frames[x]
) anddocument.images[x]
- these are Document Object/HTML Collections and the W3C standard includes onlyimages, applets, links, forms
andanchors
.So unless I'm looking in completely the wrong place, from what I can tell from the DOM-1 and DOM-2 specs, there doesn't seem to any way of arbitrarily addressing elements by tag name the way that you remember doing.
Update
The MDC entry on
HTMLCollection
is more understandable; it reads除了创建这些简写的其他 JavaScript 库之外,我不知道该语言中内置了任何简写。将其映射到您自己的简写是很简单的:
除此之外,没有对文档中所有标签的内置类似数组的访问:
http://www.javascriptkit.com/jsref/document.shtml
Other than other JavaScript libraries creating these shorthands, I am not aware of any that are built into the language. It would be trivial to map this to your own shorthand:
Other than this, there is no built-in array-like access to all of the tags in the document:
http://www.javascriptkit.com/jsref/document.shtml
创建您自己的引用
或包装器,
据我所知,支持按元素名称查询的唯一 API 是
DOM
getElementsByTagName
CSS 选择器
querySelectorAll
XPath
evaluate
E4X
(仅限 Mozilla,尚不能与 DOM 配合使用)
Create your own reference,
or a wrapper,
The only APIs I know of that support querying by element name are,
DOM
getElementsByTagName
CSS Selectors
querySelectorAll
XPath
evaluate
E4X
(mozilla only, and doesn't work with the DOM yet)