开发者

HTML - Why boolean attributes do not have boolean value?

I noticed that some elements have attributes which are boolean. I wonder why the values are not true or false? or 1 开发者_JAVA技巧and 0? Are there any reason behind why they are like this?

<option selected="selected">Ham Burger</option>

or

<input type="button" disabled="disabled" />

Thanks in advance!


In SGML, an attribute may be minimized so that its value alone is short for both the name and the value, with the only possible value for the attribute in this case obviously being the attribute's own name. HTML uses this for boolean attributes, where the presence or absence of the attribute is what's meaningful, and its value is irrelevant. But in XML, minimized attributes were disallowed, so we wound up with the awkwardness that is selected="selected" when XHTML came into vogue. If you're writing HTML rather than XHTML, you can just write selected.


The exact definition is:

Some attributes play the role of boolean variables (e.g., the selected attribute for the OPTION element). Their appearance in the start tag of an element implies that the value of the attribute is "true". Their absence implies a value of "false".

Also:

Boolean attributes may legally take a single value: the name of the attribute itself [...] In HTML, boolean attributes may appear in minimized form

Basically, this implies that there are only two possible statuses for boolean attributes, true and false, but there isn't a not set status.


For the disabled attribute I think it's the presence of the attribute that disables the element regardless of its value.

It guess one of the reasons could be to allow more values than just yes/no in the future. For instance, instead of visible=true/false, you can have visibility=visible/hidden/collapsed


the HTML standard (Not the XHTML) is to have simply selected instead of selected="selected"

See here: http://www.w3.org/TR/html4/interact/forms.html#adef-selected

When XHTML was created to allow a a better integration with XML in HTML, (see http://www.w3.org/MarkUp/2004/xhtml-faq#need), the parts that do not fit to the XML-like structure requirements of HTML were corrected. So wordings like selected got transformed into selected="selected" to fit the standard


Readability, a lot of HTML is not coded by people with computer science backgrounds so the concept of "Boolean" would be foreign to them in those terms. Also it improves readability for Computer Science and other technical users by providing reinforced clues as to the function of a given statement.


As vc74 has said, it doesn't matter what value you have for selected or disabled.

<option selected="selected">Ham Burger</option>

will do the same as

<option selected="sjkhdaskj">Ham Burger</option>


i think this is just for ease to user to specify the attribute value in most human readable form if he/she dont know what is true/false

<html>
<body>
<select>
<option>1</option>
<option selected="blah">2</option>
<option >3</option>
</select>
</body>

you see in above code i have not use selected=selected, i used what i want it still select the option value, or you can simply use <option selected>2<option>.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜