Home Web Front-end HTML Tutorial Bootstrap之表单控件状态_html/css_WEB-ITnose

Bootstrap之表单控件状态_html/css_WEB-ITnose

Jun 24, 2016 am 11:23 AM

Bootstrap中的表单控件状态主要有三种:焦点状态,禁用状态,验证状态。

 

一、焦点状态:该状态告诉用户可输入或选择东西

焦点状态通过伪类“:focus”以实现。

bootstrap.css相应源码:

.form-control:focus {    border-color: #66afe9;    outline: 0;   //删除了outline的默认样式    -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102, 175, 233, .6);  //添加了阴影效果    box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102, 175, 233, .6);}
Copy after login

使用方法:给控件添加类名“form-control”。

eg:

<input class="form-control" type="text" placeholder="不是焦点状态下效果"><input class="form-control" type="text" placeholder="焦点状态下效果">
Copy after login

效果图如下所示:(焦点状态下为蓝色边框效果)

焦点状态下,file、radio、checkbox控件的效果与普通的input空间不完全一样,因为bootstrap对它们做了特殊处理。

bootstrap.css相应源码:

input[type="file"]:focus,input[type="radio"]:focus,input[type="checkbox"]:focus {outline: thin dotted;outline: 5px auto -webkit-focus-ring-color;outline-offset: -2px;}
Copy after login

Copy after login

<strong>二、禁用状态:</strong>该状态告诉用户不可以输入或选择东西
Copy after login

禁用状态是通过在表单控件上添加"disabled"属性以实现。
Copy after login
bootstrap.css相应源码:
Copy after login

.form-control[disabled],.form-control[readonly],fieldset[disabled] .form-control {    cursor: not-allowed;    background-color: #eee;    opacity: 1;}
Copy after login

使用方法:在需要禁用的表单控件上加上"diabled"属性即可。
Copy after login
eg:
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

<input class="form-control" type="text" placeholder="不是焦点状态下效果"><input class="form-control" type="text" placeholder="表单已禁用,不能输入"  disabled>
Copy after login

效果图如下所示:
Copy after login
Copy after login

说明:禁用状态下控件背景色为灰色,且手型变为不准输入的形状,若表单控件不使用类名"form-control",则禁用的控件只有一个不准输入的手型。
Copy after login
PS:在Bootstrap中,若fieldset设置了"disabled"属性,则整个域都处于被禁用状态。
Copy after login
eg:
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

 1 <form role="form"> 2     <fieldset disabled> 3         <div class="form-group"> 4             <label for="disabledTextInput">禁用的输入框</label> 5             <input type="text" id="disabledTextInput" class="form-control" placeholder="禁止输入"> 6         </div> 7         <div class="form-group"> 8             <label for="disabledSelect">禁用的下拉框</label> 9             <select id="disabledSelect" class="form-control">10                 <option>不可选择</option>11             </select>12         </div>13         <div class="checkbox">14             <label>15                 <input type="checkbox">无法选择16             </label>17         </div>18         <button type="submit" class="btnbtn-primary">提交</button>19     </fieldset>20 </form>
Copy after login

效果如下图所示:
Copy after login

PS:对于一个禁用的域,若legend中有输入框,则此输入框是无法被禁用的。
Copy after login
eg:
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

 1 <form role="form"> 2     <fieldset disabled> 3         <legend><input type="text" class="form-control" placeholder="我没被禁用" /></legend> 4         <div class="form-group"> 5             <label for="disabledTextInput">禁用的输入框</label> 6             <input type="text" id="disabledTextInput" class="form-control" placeholder="禁止输入"> 7         </div> 8         <div class="form-group"> 9             <label for="disabledSelect">禁用的下拉框</label>10             <select id="disabledSelect" class="form-control">11                 <option>不可选择</option>12             </select>13         </div>14         <div class="checkbox">15             <label>16                 <input type="checkbox">无法选择17             </label>18         </div>19         <button type="submit" class="btnbtn-primary">提交</button>20   </fieldset> 21 </form>
Copy after login

效果图如下所示:
Copy after login
Copy after login

<strong>三、验证状态:</strong>该状态告诉用户,他们的操作是否正确
Copy after login
在Bootstrap中提供3种验证状态样式:

① .has-success : 成功状态(绿色)

② .has-error : 错误状态(红色)

③ .has-warning : 警告状态(黄色)

使用方法:在form-group容器上添加对应的状态类名即可。

eg:

 1 <form role="form"> 2     <div class="form-group has-success"> 3         <label class="control-label" for="inputSuccess1">成功状态</label> 4         <input type="text" class="form-control" id="inputSuccess1" placeholder="成功状态" > 5     </div> 6     <div class="form-group has-warning"> 7         <label class="control-label" for="inputWarning1">警告状态</label> 8         <input type="text" class="form-control" id="inputWarning1" placeholder="警告状态"> 9     </div>10     <div class="form-group has-error">11         <label class="control-label" for="inputError1">错误状态</label>12         <input type="text" class="form-control" id="inputError1" placeholder="错误状态">13     </div>14 </form>
Copy after login

说明:从效果可看出,三种样式除了颜色不同外,效果都一样。
Copy after login
在Bootstrap的表单验证中,不同状态会提供不同的icon,如成功是个对号"√",错误是个叉号"&times;"等。
Copy after login
若想让表单在不同状态下显示对应的icon,则只需在对应状态下添加类名"has-feedback"。
Copy after login
PS:类名"has-feedback"要与"has-error"、"has-warning"、"has-success"配合使用。
Copy after login
eg:
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

 1 <form role="form"> 2     <div class="form-group has-success has-feedback"> 3         <label class="control-label" for="inputSuccess">成功状态</label> 4         <input type="text" class="form-control" id="inputSuccess" placeholder="成功状态" > 5         <span class="glyphicon glyphicon-ok form-control-feedback"></span> 6     </div> 7     <div class="form-group has-warning has-feedback"> 8         <label class="control-label" for="inputWarning">警告状态</label> 9         <input type="text" class="form-control" id="inputWarning" placeholder="警告状态" >10         <span class="glyphicon glyphicon-warning-sign form-control-feedback"></span>11   </div> 12   <div class="form-group has-error has-feedback">13         <label class="control-label" for="inputError">错误状态</label>14         <input type="text" class="form-control" id="inputError" placeholder="错误状态" >15         <span class="glyphicon glyphicon-remove form-control-feedback"></span>16   </div> 17 </form>
Copy after login

效果如下所示:
Copy after login

说明:从效果图中可看出,图标都居右。
Copy after login
注:Bootstrap中的图标都是使用@face-face来制作,且必须在表单中添加个span元素来实现。
Copy after login
eg:
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

<span class="glyphicon glyphicon-remove form-control-feedback"></span>
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

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

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

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 viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

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

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

See all articles