學習使用bootstrap基本控制項(table、form、button)_javascript技巧

來源:互聯網
上載者:User

bootstrap為我們定義了簡潔易用的樣式,我們只需要很少的樣式指定,就可以完成簡約優雅的頁面展示。
本篇主要介紹以下幾個基本控制項:
1. table
2. form
3. button

1. 表格(table)依舊使用<table><thead><tbody><tr><th><td>來表現表格。有如下的類來控制table的屬性, table樣式預設會佔滿父容器

 <div class="container"> <div class="row">  <div class="col-md-8 col-md-offset-2">  <table class="table table-bordered table-striped table-hover">  <tr>  <th>標題一</th>  <th>標題二</th>  <th>標題三</th>  </tr>  <tr>  <td>1</td>  <td>2</td>  <td>3</td>  </tr>  <tr>  <td>4</td>  <td>5</td>  <td>6</td>  </tr> </table>  </div> </div> </div>

將任何.table包裹在.table-responsive中即可建立響應式表格,其會在小螢幕裝置上(小於768px)水平滾動。當螢幕大768px寬度時,水平捲軸消失。

2. 表單form, 有如個幾種樣式定義

lable與控制項要用form-group類型的div包起來,預設表單如下

 <div class="container"> <form>  <div class="form-group">  <label for="exampleInputEmail1">Email address</label>  <input type="email" class="form-control" id="exampleInputEmail1"   placeholder="Enter email">  </div>  <div class="form-group">  <label for="exampleInputPassword1">Password</label>  <input type="password" class="form-control"   id="exampleInputPassword1" placeholder="Password">  </div>  <div class="checkbox">  <label> <input type="checkbox"> Check me out  </label>  </div>  <button type="submit" class="btn btn-default">Submit</button> </form> </div>

內聯表單,為label指定sr-only類別,可隱藏掉標籤,但必須 不可省略lable.

 <div class="container"> <form class="form-inline">  <div class="form-group">  <label for="exampleInputEmail1" class="sr-only">Email address</label>  <input type="email" class="form-control" id="exampleInputEmail1"   placeholder="Enter email">  </div>  <div class="form-group">  <label for="exampleInputPassword1">Password</label>  <input type="password" class="form-control"   id="exampleInputPassword1" placeholder="Password">  </div>  <div class="checkbox">  <label> <input type="checkbox"> Check me out  </label>  </div>  <button type="submit" class="btn btn-default">Submit</button> </form> </div>

水平類型的表單,要為lable與標籤組指定長度, 採用柵格系統的布局方式。 label靠右對齊,標籤組靠左對齊。  

 <div class="container"> <form class="form-horizontal">  <div class="form-group">   <label for="exampleInputEmail1" class="col-md-2 control-label">Email   address</label>  <div class="col-md-8">   <input type="email" class="form-control" id="exampleInputEmail1"   placeholder="Enter email">  </div>  </div>  <div class="form-group" >   <label for="exampleInputPassword1" class="col-md-2 control-label">Password</label>  <div class="col-md-8">   <input type="password" class="form-control"   id="exampleInputPassword1" placeholder="Password">  </div>  </div>  <div class="checkbox col-md-offset-2">  <label> <input type="checkbox"> Check me out  </label>  </div>  <button type="submit" class="btn btn-default col-md-offset-2">Submit</button> </form> </div>

form表單驗證,bootstrap3支援表單的自訂驗證。 加入req    uired表示表單必填,node.setCustomValidity可以設定表單的自訂驗證

<div class="container"> <form class="form-horizontal">  <div class="form-group">  <label for="exampleInputEmail1" class="col-md-2 control-label">Email   address</label>  <div class="col-md-8">   <input type="email" class="form-control" id="exampleInputEmail1"   placeholder="Enter email" required>  </div>  </div>  <div class="form-group">  <label for="password1" class="col-md-2 control-label">Password</label>  <div class="col-md-8">   <input type="password" class="form-control"   id="password1" placeholder="Password" required onchange="checkPassword()">  </div>  </div><div class="form-group">  <label for="password2" class="col-md-2 control-label" onchange="checkPassword()"> Password2</label>  <div class="col-md-8">   <input type="password" class="form-control"   id="password2" placeholder="Password2" required>  </div>  </div>  <div class="checkbox col-md-offset-2">  <label> <input type="checkbox"> Check me out  </label>  </div>  <button type="submit" class="btn btn-default col-md-offset-2">Submit</button> </form> </div>  <script> function checkPassword() {  var pwd1 = $("#password1").val();  var pwd2 = $("#password2").val();  if (pwd1 != pwd2) {  document.getElementById("password1").setCustomValidity("兩次輸入的密碼不一致");  } else {  document.getElementById("password1").setCustomValidity("");  }   } </script>

3. button的樣式

使用.btn-lg、.btn-sm、.btn-xs可以獲得不同尺寸的按鈕, 給按鈕添加.btn-block可以使其充滿父節點100%的寬度,而且按鈕也變為了塊級(block)元素, <a>、<button>或<input>元素添加按鈕class。 

 <div class="container"> <button type="button" class="btn btn-default btn-block">Default</button> <button type="button" class="btn btn-primary btn-block">Primary</button> <button type="button" class="btn btn-success">Success</button> <button type="button" class="btn btn-info">Info</button> <button type="button" class="btn btn-warning">Warning</button> <button type="button" class="btn btn-danger">Danger</button> <button type="button" class="btn btn-link">連結</button> <a class="btn btn-default" href="#" role="button">Link</a> <button class="btn btn-default" type="submit">Button</button> <input class="btn btn-default" type="button" value="Input"> <input class="btn btn-default" type="submit" value="Submit"> </div>

如果大家還想深入學習,可以點擊這裡進行學習,再為大家附兩個精彩的專題:Bootstrap學習教程 Bootstrap實戰教程

以上就是本文的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.