Table of Contents
HTML 布尔属性(Boolean attributes)
checked与 checked="checked"区别
布尔属性(Boolean attributes)列表
Boolean attributes in React
Home Web Front-end HTML Tutorial HTML Boolean Attributes_html/css_WEB-ITnose

HTML Boolean Attributes_html/css_WEB-ITnose

Jun 21, 2016 am 08:48 AM

有时在阅读别人的 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="">
Copy after login

如果表示不选中,则不能出现 checked属性:

<input type="checkbox">
Copy after login

个人对于空字符串可作为合法值不太赞同,感觉会给人一种不选中的误导。

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
Copy after login

上面列表获取方法: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
Copy after login

Boolean attributes in React

React的 JSX 语法与 HTML 有所不同,在 JSX 中,可以传入 true表示设置该布尔属性,传入 false表示不设置该属性,如:

表示选中复选框:

<input type="checkbox" checked /><input type="checkbox" checked={true} />
Copy after login

表示不选中复选框:

<input type="checkbox" /><input type="checkbox" checked={false} />
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

See all articles