Are there any advantages to 开发者_开发问答using <div id="here" ...
instead of <div name="here" ...
?
Are they both referenced to as #here?
Here are some differences between both:
name has never been a div element attribute.
name as used on the form controls (input, textarea, select, and button elements) is radically different from the id attribute on named elements. In this case, the name attribute relates to how data is labeled when sent to server, and multiple elements may share the same name.
The id attribute on the other hand is for identifying one unique element for the purposes of scripting, styling, or addressing.
The use of the name attribute on other elements than form controls was in HTML 4.01 the same as that of id, but it allowed for a wider set of characters than the id attribute and wasn't controlled in quite the same way. Because of this ambiguity the W3C decided to deprecate/remove the name attribute on those elements in favor for the unambigous id attribute in XHTML.
This is also connected to another detail of XML however - only one attribute of any element may be of the type ID, which would not be the case if they let name stay on the element, but corrected the ambiguity problems.
As the name attribute didn't work the same on those two sets of elements, it was best to remove one of them.
In short, for backwards compatibility, you should use the name and id attribute both, both set to the same value, for all elements except form controls if you use HTML 4.01 or XHTML 1.0 Transitional. If you use XHTML 1.0 Strict or later you should use only id. For form controls, you should use name for what you want the form to send to the server and for DOM 0 access, and only use id for styling, DOM1-3 access or addressing reasons
It depends on where you are going to use them.
Usually, id
of an element is unique while multiple elements can share the same name
.
Id is referenced as #here
, and name is referenced as [name=here]
.
They are not interchangeable, even if they sometimes appear to be.
Name should only exist on form input fields. It is what that tag will cause the browser to pass in the submission as the name of the field. As Tomalak noted in a comment, DIV actually does not have a Name attribute.
ID is the unique identifier for the DOM, to manipulate or refer to the tag; for example, in JavaScript.
The "id" attribute assigns an identifier to the associated element. This identifier must be unique in the document and can be used to refer to that element.
Check div element.
You are probably thinking about the input tag. It offers the name attribute to identify which input is what when the form is submitted.
id would be only used to style the input, whereas name would not.
One thing of importance that hasn't been mentioned in the other answers is that a form with a name attribute gets added as a variable to the document object in JavaScript. Any form controls with a name inside that form are accessible as child objects on the form element.
So if, for example, you had some HTML like the following:
<form name="myForm">
<input name="myInput" type="text" />
</form>
You could access the form with document.myForm
or access the input element with document.myForm.myInput
.
See an example of this idea in action on JSFiddle.
The answer is:
- the id attribute is to be unique across the entirety of the HTML document, and
- the name attribute has no such requirement.
By convention, the name value is used with forms to help with the processing of forms, particularly with radio buttons. How you use the name attribute depends on your use case. If you need some other tag for your use case, you can of course create a new attribute and use that as you will; pick a name for your attribute that will likely be very unique!
精彩评论