HTML输入 - 名称与ID [英] HTML input - name vs. id
本文介绍了HTML输入 - 名称与ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当使用HTML < input>
标记时,使用名称
和 id
属性,特别是我发现它们有时被命名为相同?
解决方案
在HTML4.01中:
名称属性
- 仅对
< a>
,< form>
,< iframe>
,< img>
,< map>
,< input>
,< select>
,< textarea>
- 名称不必是唯一的,并且可以用于将元素分组在一起,例如单选按钮&复选框
- 不能在URL中引用,尽管JavaScript和PHP可以看到URL有变通方法
- 在JS中引用
getElementsByName()
- 共享与
id
属性相同的命名空间
- 必须以字母开头
- 根据规格是区分大小写的,但大多数现代浏览器似乎不遵循这一点
- 用于表单元素提交信息。只有具有
名称
属性的输入标记才会提交给服务器 - 对除
< base>
< html>
,< head>
,< meta> code>
< param>
,< script> ,
<风格> ,
<标题>
- 每个标识在 / em>在浏览器中呈现,可能或不可能都在同一个文件中
- 可用作URL中的锚点引用
- 在CSS或URL中引用
#
sign - 在JS中用
getElementById()
和jQuery由$(#< id>)
- 空格作为名称属性
- 必须包含至少一个字符
- 必须以字母开头
- 包含字母,数字以外的任何内容s,下划线(
_
),破折号(-
),冒号(:
)或句点(。
) - 不区分大小写
- 无效于
< form>
之后 - XHTML表示它必须全部小写,但大多数浏览器不会遵循这一点
- 对任何元素有效
- XHTML表示它必须全部小写,但大多数浏览器不会请遵循
- Valid only on
<a>
,<form>
,<iframe>
,<img>
,<map>
,<input>
,<select>
,<textarea>
- Name does not have to be unique, and can be used to group elements together such as radio buttons & checkboxes
- Can not be referenced in URL, although as JavaScript and PHP can see the URL there are workarounds
- Is referenced in JS with
getElementsByName()
- Shares the same namespace as the
id
attribute - Must begin with a letter
- According to specs is case sensitive, but most modern browsers don't seem to follow this
- Used on form elements to submit information. Only input tags with a
name
attribute are submitted to the server - Valid on any element except
<base>
,<html>
,<head>
,<meta>
,<param>
,<script>
,<style>
,<title>
- Each Id should be unique in the page as rendered in the browser, which may or may not be all in the same file
- Can be used as anchor reference in URL
- Is referenced in CSS or URL with
#
sign - Is referenced in JS with
getElementById()
, and jQuery by$(#<id>)
- Shares same name space as name attribute
- Must contain at least one character
- Must begin with a letter
- Must not contain anything other than letters, numbers, underscores (
_
), dashes (-
), colons (:
), or periods (.
) - Is case insensitive
- Not valid on
<form>
anymore - XHTML says it must be all lowercase, but most browsers don't follow that
- Valid on any element
- XHTML says it must be all lowercase, but most browsers don't follow that
Id 属性
在(X)HTML5中,除了以下内容外,所有内容都是相同的:
名称属性
Id 属性
这个问题是在HTML4.01为标准时编写的,许多浏览器和功能都与今天不同。
>When using the HTML <input>
tag, what is the difference between the use of the name
and id
attributes especially that I found that they are sometimes named the same?
解决方案
In HTML4.01:
Name Attribute
Id Attribute
In (X)HTML5, everything is the same except:
Name Attribute
Id Attribute
This question was written when HTML4.01 was the norm, and many browsers and features were different from today.
这篇关于HTML输入 - 名称与ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文