通过标签名称简写获取元素?

发布于 2024-11-03 05:25:25 字数 423 浏览 1 评论 0 原文

我不知道它叫什么,但我知道有一种方法可以根据元素的标签获取元素,而无需 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木落 2024-11-10 05:25:25

正如答案中其他地方提到的,这实际上与 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-1DOM-2 规范,似乎没有任何方法可以像您记得的那样通过标签名称任意寻址元素。

更新

HTMLCollection 上的 MDC 条目 更容易理解;它读着

下面列出了返回 HTMLCollection 的每个项目(及其特定属性): 文档(图像、小程序、链接、表单、锚点);形式(元素);地图(区域);表(行、tBodies);表部分(行);行(单元格)

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 be window.frames[x]) and document.images[x] - these are Document Object/HTML Collections and the W3C standard includes only images, applets, links, forms and anchors.

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

The following lists each item (and its specific properties) which return an HTMLCollection: Document (images, applets, links, forms, anchors); form (elements); map (areas); table (rows, tBodies); tableSection (rows); row (cells)

北城孤痞 2024-11-10 05:25:25

除了创建这些简写的其他 JavaScript 库之外,我不知道该语言中内置了任何简写。将其映射到您自己的简写是很简单的:

var $ = document.getElementsByTagName;

// you can then use it like so:
$('SPAN').// and so on

除此之外,没有对文档中所有标签的内置类似数组的访问:

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:

var $ = document.getElementsByTagName;

// you can then use it like so:
$('SPAN').// and so on

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

临走之时 2024-11-10 05:25:25

创建您自己的引用

document.tag = document.getElementsByTagName;

或包装器,

function tag(name) {
    return document.getElementsByTagName(name);
}

据我所知,支持按元素名称查询的唯一 API 是

DOM

getElementsByTagName

CSS 选择器

querySelectorAll

XPath

evaluate

E4X

(仅限 Mozilla,尚不能与 DOM 配合使用)

Create your own reference,

document.tag = document.getElementsByTagName;

or a wrapper,

function tag(name) {
    return document.getElementsByTagName(name);
}

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文