2013-03-05 71 views
-1

我做了一些jQuery的代码,取名称属性!一个DIV!并以某种方式使用它。我可以在哪里使用“name”属性(在html中)?

如果一方面我在每一个浏览器中尝试了这段代码(firefox,chrome,即9/8/7,safari,opera,android ......所有最后一次更新),并且它在其他我看到的 - 用谷歌搜索 - “名称”被认为是只有表格输入, textarea,,和其他类似。

我真的可以像我一样使用它吗?在每个我想要的html标签?或者我会永远燃烧在地狱?

预先感谢您


看到你的答案,我决定存储DATAS其他方式(如添加类,甚至使用jQuery检索他们的名字)。

+0

它的使用在'了'元素也是多余的,在页面导航中使用元素的“id”。 – 2013-03-05 15:05:24

回答

2
<a>, <applet>, <button>, <form>, <frame>, <iframe>, <img>, <input>, <map>, <meta>, <object>, <param>, <select>, and <textarea> 

https://developer.mozilla.org/en-US/docs/DOM/element.name

据我所知,名字是主要用于配对的元素与标签(在这种精神,分组单选按钮)。

编辑:提及迈克的评论。名称也用于通过GET和POST以键/值对的方式提交表单。


更新:它可能是值得考虑的this post

OMG Ponies对主题的一些优秀的信息:

My understanding is that when Netscape created Javascript, it used the name attribute. The HTML spec however decided to go with id, but kept name for backwards compatibility. IME, using the name attribute was required for Internet Explorer 6 support because the javascript engine in IE wouldn't read the id attribute - only the name though both were defined.

+1

我相信'for'属性是用于将标签与元素配对使用的。名称属性是浏览器用于设置表单帖子上的发布变量的名称。 – 2013-03-06 15:13:42

+0

好吧,它会是,但您说得对,名称也用于POST上的键/值对 – 2013-03-06 15:26:53

4

你可以使用任何你想要的属性名称 - 根据规范,但“名”未在div标签有效的属性,让你的标记在技术上是无效的。

如果作为一个例子,你把这个网站:

<!DOCTYPE html> 
<head> 
    <title>test</title> 
</head> 
<body> 
    <div name="mydiv" class="myclass"></div> 
</body> 

并验证它here,你会看到你收到此错误:

Line 6, Column 41: Attribute name not allowed on element div at this point. 
     <div name="mydiv" class="myclass"></div> 
+0

谢谢,我没有意识到也有来自w3.org的验证器 – 2013-03-06 00:37:27

相关问题