gpt4 book ai didi

javascript - 为什么他们弃用 HTML 元素的 'name' 属性?

转载 作者:行者123 更新时间:2023-11-30 17:18:28 24 4
gpt4 key购买 nike

From what I understand ,在 HTML5 中我们应该开始使用 id 属性来引用 a 元素,而不是 name 属性。这样做的问题是,还有一个约束,即每个文档的 id 应该是唯一的。

现在,我看到很多情况下您想要引用一组相关的 a 元素(通常在 JS 中)。但是,通过删除 name 属性,我们不得不重新使用相同的 id,或者使用 class 名称。类应该与演示有关,因此如果您出于与演示无关的原因引用您的 a,这也是一个不完美的解决方案。

我想另一种 HTML5 替代方法是使用 data-name 之类的东西,但这看起来也很老套。有什么我想念的吗?是否存在 W3 的 secret 阴谋以完全淘汰 name

编辑:对于那些说“类(class)不一定需要与演示有关”的人,我很难找到特定的来源,但我知道我至少阅读了几篇强调这一点的博客/文章.我有一种感觉,那些人不是 JS 程序员......

另外:感谢所有解释清楚、深思熟虑的答案!

最佳答案

啊,HTML。你有多矛盾。

问题似乎是混淆了 anchor 的 name 属性和输入的 name 属性。回到 HTML5 之前的时代,您需要一种方法来区分传出链接(通过 href)和内部文档链接(# 书签)。

那么,他们将如何实现这一目标?此处指定:http://www.w3.org/TR/html401/struct/links.html

By activating these links (by clicking with the mouse, through keyboard input, voice commands, etc.), users may visit these resources. Note that the href attribute in each source anchor specifies the address of the destination anchor with a URI.

The destination anchor of a link may be an element within an HTML document. The destination anchor must be given an anchor name and any URI addressing this anchor must include the name as its fragment identifier.

Destination anchors in HTML documents may be specified either by the A element (naming it with the name attribute), or by any other element (naming with the id attribute).

是的,给一个 anchor 一个名称属性(作为片段标识符),另一个 anchor 可以链接到。

只有在 HTML5 中,他们决定任何元素(a 除外)都可以充当片段标识符(这是有道理的 - 应该只有一个片段可以链接到......) -这就是他们弃用 name 属性的原因。

至于如何选择? class 属性非常好(当然,它用于样式设置,但它是否使它成为纯粹的表现形式?)

另一个引用(来自 w3c:http://www.w3.org/TR/html401/struct/global.html#adef-class)

The class attribute, on the other hand, assigns one or more class names to an element; the element may be said to belong to these classes. A class name may be shared by several element instances. The class attribute has several roles in HTML:

*As a style sheet selector (when an author wishes to assign style information to a set of elements).

*For general purpose processing by user agents.

关于javascript - 为什么他们弃用 HTML <a> 元素的 'name' 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25633828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com