HTML attribute reference - HTML: HyperText Markup Language 编辑

Elements in HTML have attributes; these are additional values that configure the elements or adjust their behavior in various ways to meet the criteria the users want.

Attribute list

Attribute NameElementsDescription
accept<form>, <input>List of types the server accepts, typically a file type.
accept-charset<form>List of supported charsets.
accesskeyGlobal attributeKeyboard shortcut to activate or add focus to the element.
action<form>The URI of a program that processes the information submitted via the form.
align<applet>, <caption>, <col>, <colgroup>, <hr>, <iframe>, <img>, <table>, <tbody>, <td>, <tfoot> , <th>, <thead>, <tr>Specifies the horizontal alignment of the element.
allow<iframe>Specifies a feature-policy for the iframe.
alt<applet>, <area>, <img>, <input>Alternative text in case an image can't be displayed.
async<script>Executes the script asynchronously.
autocapitalizeGlobal attributeSets whether input is automatically capitalized when entered by user
autocomplete<form>, <input>, <select>, <textarea>Indicates whether controls in this form can by default have their values automatically completed by the browser.
autofocus<button>, <input>, <keygen>, <select>, <textarea>The element should be automatically focused after the page loaded.
autoplay<audio>, <video>The audio or video should play as soon as possible.
background<body>, <table>, <td>, <th>Specifies the URL of an image file. Note: Although browsers and email clients may still support this attribute, it is obsolete. Use CSS background-image instead.
bgcolor<body>, <col>, <colgroup>, <marquee>, <table>, <tbody>, <tfoot>, <td>, <th>, <tr>

Background color of the element.

Note: This is a legacy attribute. Please use the CSS background-color property instead.

border<img>, <object>, <table>

The border width.

Note: This is a legacy attribute. Please use the CSS border property instead.

buffered<audio>, <video>Contains the time range of already buffered media.
capture<input>From the HTML Media Capture
The definition of 'media capture' in that specification.
spec, specifies a new file can be captured.
challenge<keygen>A challenge string that is submitted along with the public key.
charset<meta>, <script>Declares the character encoding of the page or script.
checked<command>, <input>Indicates whether the element should be checked on page load.
cite<blockquote>, <del>, <ins>, <q>Contains a URI which points to the source of the quote or change.
classGlobal attributeOften used with CSS to style elements with common properties.
code<applet>Specifies the URL of the applet's class file to be loaded and executed.
codebase<applet>This attribute gives the absolute or relative URL of the directory where applets' .class files referenced by the code attribute are stored.
color<basefont>, <font>, <hr>

This attribute sets the text color using either a named color or a color specified in the hexadecimal #RRGGBB format.

Note: This is a legacy attribute. Please use the CSS color property instead.

cols<textarea>Defines the number of columns in a textarea.
colspan<td>, <th>The colspan attribute defines the number of columns a cell should span.
content<meta>A value associated with http-equiv or name depending on the context.
contenteditableGlobal attributeIndicates whether the element's content is editable.
contextmenuGlobal attributeDefines the ID of a <menu> element which will serve as the element's context menu.
controls<audio>, <video>Indicates whether the browser should show playback controls to the user.
coords<area>A set of values specifying the coordinates of the hot-spot region.
crossorigin<audio>, <img>, <link>, <script>, <video>How the element handles cross-origin requests
csp This is an experimental API that should not be used in production code.<iframe>Specifies the Content Security Policy that an embedded document must agree to enforce upon itself.
data<object>Specifies the URL of the resource.
data-*Global attributeLets you attach custom attributes to an HTML element.
datetime<del>, <ins>, <time>Indicates the date and time associated with the element.
decoding<img>Indicates the preferred method to decode the image.
default<track>Indicates that the track should be enabled unless the user's preferences indicate something different.
defer<script>Indicates that the script should be executed after the page has been parsed.
dirGlobal attributeDefines the text direction. Allowed values are ltr (Left-To-Right) or rtl (Right-To-Left)
dirname<input>, <textarea>
disabled<button>, <command>, <fieldset>, <input>, <keygen>, <optgroup>, <option>, <select>, <textarea>Indicates whether the user can interact with the element.
download<a>, <area>Indicates that the hyperlink is to be used for downloading a resource.
draggableGlobal attributeDefines whether the element can be dragged.
dropzoneGlobal attributeIndicates that the element accepts the dropping of content onto it.
enctype<form>Defines the content type of the form data when the method is POST.
enterkeyhint This is an experimental API that should not be used in production code.<textarea>, contenteditableThe enterkeyhint specifies what action label (or icon) to present for the enter key on virtual keyboards. The attribute can be used with form controls (such as the value of textarea elements), or in elements in an editing host (e.g., using contenteditable attribute).
for<label>, <output>Describes elements which belongs to this one.
form<button>, <fieldset>, <input>, <keygen>, <label>, <meter>, <object>, <output>, <progress>, <select>, <textarea>Indicates the form that is the owner of the element.
formaction<input>, <button>Indicates the action of the element, overriding the action defined in the <form>.
formenctype<button>, <input>If the button/input is a submit button (type="submit"), this attribute sets the encoding type to use during form submission. If this attribute is specified, it overrides the enctype attribute of the button's form owner.
formmethod<button>, <input>If the button/input is a submit button (type="submit"), this attribute sets the submission method to use during form submission (GET, POST, etc.). If this attribute is specified, it overrides the method attribute of the button's form owner.
formnovalidate<button>, <input>If the button/input is a submit button (type="submit"), this boolean attribute specifies that the form is not to be validated when it is submitted. If this attribute is specified, it overrides the novalidate attribute of the button's form owner.
formtarget<button>, <input>If the button/input is a submit button (type="submit"), this attribute specifies the browsing context (for example, tab, window, or inline frame) in which to display the response that is received after submitting the form. If this attribute is specified, it overrides the target attribute of the button's form owner.
headers<td>, <th>IDs of the <th> elements which applies to this element.
height<canvas>, <embed>, <iframe>, <img>, <input>, <object>, <video>

Specifies the height of elements listed here. For all other elements, use the CSS height property.

Note: In some instances, such as <div>, this is a legacy attribute, in which case the CSS height property should be used instead.

hiddenGlobal attributePrevents rendering of given element, while keeping child elements, e.g. script elements, active.
high<meter>Indicates the lower bound of the upper range.
href<a>, <area>, <base>, <link>The URL of a linked resource.
hreflang<a>, <area>, <link>Specifies the language of the linked resource.
http-equiv<meta>Defines a pragma directive.
icon<command>Specifies a picture which represents the command.
idGlobal attributeOften used with CSS to style a specific element. The value of this attribute must be unique.
importance This is an experimental API that should not be used in production code.<iframe>, <img>, <link>, <script>Indicates the relative fetch priority for the resource.
integrity<link>, <script>

Specifies a Subresource Integrity value that allows browsers to verify what they fetch.

intrinsicsize This deprecated API should no longer be used, but will probably still work.<img>This attribute tells the browser to ignore the actual intrinsic size of the image and pretend it’s the size specified in the attribute.
inputmode<textarea>, contenteditableProvides a hint as to the type of data that might be entered by the user while editing the element or its contents. The attribute can be used with form controls (such as the value of textarea elements), or in elements in an editing host (e.g., using contenteditable attribute).
ismap<img>Indicates that the image is part of a server-side image map.
itempropGlobal attribute
keytype<keygen>Specifies the type of key generated.
kind<track>Specifies the kind of text track.
label<optgroup>, <option>, <track>Specifies a user-readable title of the element.
langGlobal attributeDefines the language used in the element.
language<script>Defines the script language used in the element.
loading This is an experimental API that should not be used in production code.<img>, <iframe>Indicates if the element should be loaded lazily (loading="lazy") or loaded immediately (loading="eager"). WIP: WHATWG PR #3752
list<input>Identifies a list of pre-defined options to suggest to the user.
loop<audio>, <bgsound>, <marquee>, <video>Indicates whether the media should start playing from the start when it's finished.
low<meter>Indicates the upper bound of the lower range.
manifest<html>Specifies the URL of the document's cache manifest. Note: This attribute is obsolete, use <link rel="manifest"> instead.
max<input>, <meter>, <progress>Indicates the maximum value allowed.
maxlength<input>, <textarea>Defines the maximum number of characters allowed in the element.
minlength<input>, <textarea>Defines the minimum number of characters allowed in the element.
media<a>, <area>, <link>, <source>, <style>Specifies a hint of the media for which the linked resource was designed.
method<form>Defines which HTTP method to use when submitting the form. Can be GET (default) or POST.
min<input>, <meter>Indicates the minimum value allowed.
multiple<input>, <select>Indicates whether multiple values can be entered in an input of the type email or file.
muted<audio>, <video>Indicates whether the audio will be initially silenced on page load.
name<button>, <form>, <fieldset>, <iframe>, <input>, <keygen>, <object>, <output>, <select>, <textarea>, <map>, <meta>, <param>Name of the element. For example used by the server to identify the fields in form submits.
novalidate<form>This attribute indicates that the form shouldn't be validated when submitted.
open<details>Indicates whether the details will be shown on page load.
optimum<meter>Indicates the optimal numeric value.
pattern<input>Defines a regular expression which the element's value will be validated against.
ping<a>, <area>The ping attribute specifies a space-separated list of URLs to be notified if a user follows the hyperlink.
placeholder<input>, <textarea>Provides a hint to the user of what can be entered in the field.
poster<video>A URL indicating a poster frame to show until the user plays or seeks.
preload<audio>, <video>Indicates whether the whole resource, parts of it or nothing should be preloaded.
radiogroup<command>
readonly<input>, <textarea>Indicates whether the element can be edited.
referrerpolicy<a>, <area>, <iframe>, <img>, <link>, <script>Specifies which referrer is sent when fetching the resource.
rel<a>, <area>, <link>Specifies the relationship of the target object to the link object.
required<input>, <select>, <textarea>Indicates whether this element is required to fill out or not.
reversed<ol>Indicates whether the list should be displayed in a descending order instead of a ascending.
rows<textarea>Defines the number of rows in a text area.
rowspan<td>, <th>Defines the number of rows a table cell should span over.
sandbox<iframe>Stops a document loaded in an iframe from using certain features (such as submitting forms or opening new windows).
scope<th>Defines the cells that the header test (defined in the th element) relates to.
scoped<style>
selected<option>Defines a value which will be selected on page load.
shape<a>, <area>
size<input>, <select>Defines the width of the element (in pixels). If the element's type attribute is text or password then it's the number of characters.
sizes<link>, <img>, <source>
slotGlobal attributeAssigns a slot in a shadow DOM shadow tree to an element.
span<col>, <colgroup>
spellcheckGlobal attributeIndicates whether spell checking is allowed for the element.
src<audio>, <embed>, <iframe>, <img>, <input>, <script>, <source>, <track>, <video>The URL of the embeddable content.
srcdoc<iframe>
srclang<track>
srcset<img>, <source>One or more responsive image candidates.
start<ol>Defines the first number if other than 1.
step<input>
styleGlobal attributeDefines CSS styles which will override styles previously set.
summary<table>
tabindexGlobal attributeOverrides the browser's default tab order and follows the one specified instead.
target<a>, <area>, <base>, <form>Specifies where to open the linked document (in the case of an <a> element) or where to display the response recieved (in the case of a <form> element)
titleGlobal attributeText to be displayed in a tooltip when hovering over the element.
translateGlobal attributeSpecify whether an element’s attribute values and the values of its Text node children are to be translated when the page is localized, or whether to leave them unchanged.
type<button>, <input>, <command>, <embed>, <object>, <script>, <source>, <style>, <menu>Defines the type of the element.
usemap<img>, <input>, <object>
value<button>, <data>, <input>, <li>, <meter>, <option>, <progress>, <param>Defines a default value which will be displayed in the element on page load.
width<canvas>, <embed>, <iframe>, <img>, <input>, <object>, <video>

For the elements listed here, this establishes the element's width.

Note: For all other instances, such as <div>, this is a legacy attribute, in which case the CSS width property should be used instead.

wrap<textarea>Indicates whether the text should be wrapped.

Content versus IDL attributes

In HTML, most attributes have two faces: the content attribute and the IDL (Interface Definition Language) attribute.

The content attribute is the attribute as you set it from the content (the HTML code) and you can set it or get it via element.setAttribute() or element.getAttribute(). The content attribute is always a string even when the expected value should be an integer. For example, to set an <input> element's maxlength to 42 using the content attribute, you have to call setAttribute("maxlength", "42") on that element.

The IDL attribute is also known as a JavaScript property. These are the attributes you can read or set using JavaScript properties like element.foo. The IDL attribute is always going to use (but might transform) the underlying content attribute to return a value when you get it and is going to save something in the content attribute when you set it. In other words, the IDL attributes, in essence, reflect the content attributes.

Most of the time, IDL attributes will return their values as they are really used. For example, the default type for <input> elements is "text", so if you set input.type="foobar", the <input> element will be of type text (in the appearance and the behavior) but the "type" content attribute's value will be "foobar". However, the type IDL attribute will return the string "text".

IDL attributes are not always strings; for example, input.maxlength is a number (a signed long). When using IDL attributes, you read or set values of the desired type, so input.maxlength is always going to return a number and when you set input.maxlength, it wants a number. If you pass another type, it is automatically converted to a number as specified by the standard JavaScript rules for type conversion.

IDL attributes can reflect other types such as unsigned long, URLs, booleans, etc. Unfortunately, there are no clear rules and the way IDL attributes behave in conjunction with their corresponding content attributes depends on the attribute. Most of the time, it will follow the rules laid out in the specification, but sometimes it doesn't. HTML specifications try to make this as developer-friendly as possible, but for various reasons (mostly historical), some attributes behave oddly (select.size, for example) and you should read the specifications to understand how exactly they behave.

Boolean Attributes

Some content attributes (e.g. required, readonly, disabled) are called boolean attributes. If a boolean attribute is present, its value is true, and if it’s absent, its value is false.

HTML5 defines restrictions on the allowed values of boolean attributes: If the attribute is present, its value must either be the empty string (equivalently, the attribute may have an unassigned value), or a value that is an ASCII case-insensitive match for the attribute’s canonical name, with no leading or trailing whitespace. The following examples are valid ways to mark up a boolean attribute:

<div itemscope> This is valid HTML but invalid XML. </div>
<div itemscope=itemscope> This is also valid HTML but invalid XML. </div>
<div itemscope=""> This is valid HTML and also valid XML. </div>
<div itemscope="itemscope"> This is also valid HTML and XML, but perhaps a bit verbose. </div>

To be clear, the values "true" and "false" are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether. This restriction clears up some common misunderstandings: With checked="false" for example, the element’s checked attribute would be interpreted as true because the attribute is present.

See also

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:75 次

字数:62920

最后编辑:7 年前

编辑次数:0 次

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