disabled & disabled = "disabled"的差異?

在寫表單驗證功能中,

disabled & disabled = "disabled"的差異在這篇有說明到:

Correct value for disabled attribute
http://stackoverflow.com/questions/6961526/correct-value-for-disabled-attribute


以下擷取最佳解答:

1. For XHTML, <input type="text" disabled="disabled" /> is the valid markup.

2. For HTML5, <input type="text" disabled /> is valid and used by W3C on their samples.

3. In fact, both ways works on all major browsers.



另外也擷取輔助回答, 因為問問題的人的寫法本身就可以再好一些XD:


HTML5 spec:

http://www.w3.org/TR/html5/forms.html#enabling-and-disabling-form-controls:-the-disabled-attribute :

The checked content attribute is a boolean attribute


http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes :

The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.


Conclusion:

The following are valid, equivalent and true:

<input type="text" disabled />
<input type="text" disabled="" />
<input type="text" disabled="disabled" />
<input type="text" disabled="DiSaBlEd" />


The following are invalid:

<input type="text" disabled="0" />
<input type="text" disabled="1" />
<input type="text" disabled="false" />
<input type="text" disabled="true" />


The absence of the attribute is the only valid syntax for false:

<input type="text" />


Recommendation

If you care about writing valid XHTML, use disabled="disabled", since <input disabled> is invalid and other alternatives are less readable. Else, just use <input disabled> as it is shorter.



留言

熱門文章