有时在阅读别人的 HTML 代码时,会发现在写 / <input type="checkbox">/
前几天,我突然发现我没有搞清楚 HTML 布尔属性假如有值的话,哪些是合法值,我竟然认为 空字符串和 "false"表示 假值,然后看规范才明白不是这样。
HTML 布尔属性(Boolean attributes)
HTML5 规范说:
… A number of attributes are 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.
以及:
The values “true” and “false” are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether.
所以,如果要表示 <input type="checkbox">选中的写法有:
<input type="checkbox" checked><input type="checkbox" checked="checked"><input type="checkbox" checked="">
如果表示不选中,则不能出现 checked属性:
<input type="checkbox">
个人对于空字符串可作为合法值不太赞同,感觉会给人一种不选中的误导。
checked与 checked="checked"区别
在 W3C 的 HTML 4.01 规范中已经有 Boolean attributes 的说明,里面提到了:
Authors should be aware that many user agents only recognize the minimized form of boolean attributes and not the full form.
当然,现在主流的浏览器都支持 checked="checked"这种写法的,所以实际使用效果没有区别。
其区别主要在于: checked字符少,简洁直观;而使用 checked="checked"的理由主要是在 XHTML 中,只写 checked不合法,但是我们通常是在写 HTML5,所以没什么理由写 checked="checked"。
布尔属性(Boolean attributes)列表
截止该文章书写时有 39 个:
allowFullscreenasyncautofocusautoplaycheckedcompactcontrolsdeclaredefaultdefaultCheckeddefaultMuteddefaultSelecteddeferdisableddraggableenabledformNoValidatehiddenimageSmoothingEnabledindeterminateisMaploopmultiplemutednoHrefnoResizenoShadenoValidatenoWrapopenpauseOnExitreadOnlyrequiredreversedselectedspellchecktranslatetrueSpeedtypeMustMatch
上面列表获取方法:WHATWG HTML 规范在 Github 上面的仓库( https://github.com/whatwg/html)
git clone https://github.com/whatwg/htmlcd html# cat source | grep "attribute boolean" 之后手工处理,或执行下面代码cat source | grep "attribute boolean" | grep -v "readonly attribute boolean" | grep -oE "\">[A-Za-z]{1,}</span>" | awk -F'>|<' '{print $2}' | sort | uniq
Boolean attributes in React
React的 JSX 语法与 HTML 有所不同,在 JSX 中,可以传入 true表示设置该布尔属性,传入 false表示不设置该属性,如:
表示选中复选框:
<input type="checkbox" checked /><input type="checkbox" checked={true} />
表示不选中复选框:
<input type="checkbox" /><input type="checkbox" checked={false} />

公眾號網頁更新緩存,這玩意兒,說簡單也簡單,說複雜也夠你喝一壺的。你辛辛苦苦更新了公眾號文章,結果用戶打開還是老版本,這滋味,誰受得了?這篇文章,咱就來扒一扒這背後的彎彎繞繞,以及如何優雅地解決這個問題。讀完之後,你就能輕鬆應對各種緩存難題,讓你的用戶始終體驗到最新鮮的內容。先說點基礎的。網頁緩存,說白了就是瀏覽器或者服務器為了提高訪問速度,把一些靜態資源(比如圖片、CSS、JS)或者頁面內容存儲起來。下次訪問時,直接從緩存裡取,不用再重新下載,速度自然快。但這玩意兒,也是個雙刃劍。新版本上線,

本文討論了使用HTML5表單驗證屬性,例如必需的,圖案,最小,最大和長度限制,以直接在瀏覽器中驗證用戶輸入。

本文展示了使用CSS為網頁中添加有效的PNG邊框。 它認為,與JavaScript或庫相比,CSS提供了出色的性能,詳細介紹瞭如何調整邊界寬度,樣式和顏色以獲得微妙或突出的效果

本文討論了html&lt; datalist&gt;元素,通過提供自動完整建議,改善用戶體驗並減少錯誤來增強表格。Character計數:159

本文討論了HTML&lt; meter&gt;元素,用於在一個範圍內顯示標量或分數值及其在Web開發中的常見應用。它區分了&lt; meter&gt;從&lt; progress&gt;和前

本文解釋了HTML5&lt; time&gt;語義日期/時間表示的元素。 它強調了DateTime屬性對機器可讀性(ISO 8601格式)的重要性,並在人類可讀文本旁邊,增強Accessibilit

本文討論了HTML&lt; Progress&gt;元素,其目的,樣式和與&lt; meter&gt;元素。主要重點是使用&lt; progress&gt;為了完成任務和LT;儀表&gt;對於stati


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3漢化版
中文版,非常好用

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)