1. 程式人生 > >bootstrap模態框自動彈出與手動彈出詳解

bootstrap模態框自動彈出與手動彈出詳解

bootstrap模態框自動彈出功能與js 無關,手動彈出則需要js去控制

兩者的區別:自動彈出需要在按鈕多設定一個data-target屬性target到模態框的id , JS控制彈出的話需要在按鈕加個click事件$("#Mymodal").modal("show"),關閉時還是要手動關閉$("#Mymodal").modal("hide")

1.bootstrap模態框自動彈出演示

<!doctype html>  
<html>  
<head>  
<meta charset="utf-8">  
<title>bootstrap 彈出模態框</title>  
<link rel="stylesheet" href="bootstrap-3.3.5-dist/css/bootstrap.css">  
<script src="bootstrap-3.3.5-dist/js/jquery-2.1.4.min.js"></script>  
<script src="bootstrap-3.3.5-dist/js/bootstrap.js"></script> 
<script>
	$(document).ready(function(){
		$("#Mymodal").click(function(){
			$("#new").modal("show")
		})
	})
	</script> 
</head>  
<body>  
<button class="btn btn-success" data-toggle="modal" data-target="#new"> //多了data-target="#new"

 <span style="color:#ff0000;"></span>
自動彈出模態框
 </button>  
 
 <button class="btn btn-success" data-toggle="modal" id="Mymodal" >//沒有data-target屬性,但是有上面的JS  click事件

 <span style="color:#ff0000;"></span>
js控制彈出模態框
 </button> 
  
 <div class="modal fade" id="new">
            <div class="modal-dialog">  
              <div class="modal-content">  
                <div class="modal-header"> Create a new user </div>  
                <form class="form-horizontal">  
                  <div class="modal-body">  
                    <div class="form-group">  
                      <label for="inputEmail" class="col-sm-2 control-label">Email</label>  
                      <div class="col-sm-10">  
                        <input id="inputEmail" type="text" class="form-control list-inline" placeholder="Email"/>  
                      </div>  
                    </div>  
                    <div class="form-group">  
                      <label for="inputpwd" class="col-sm-2 control-label">Password</label>  
                      <div class="col-sm-10">  
                        <input id="inputpwd" type="text" class="form-control list-inline" placeholder="Password"/>  
                      </div>  
                    </div>  
                    <div class="form-group">  
                      <label for="inputUsername" class="col-sm-2 control-label">Username</label>  
                      <div class="col-sm-10">  
                        <input id="inputUsername" type="text" class="form-control list-inline" placeholder="Username"/>  
                      </div>  
                    </div>  
                    <div class="form-group">  
                      <label for="inputBirthday" class="col-sm-2 control-label">Birthday</label>  
                      <div class="col-sm-10">  
                        <input id="inputBirthday" type="text" class="form-control list-inline" placeholder="Birthday"/>  
                      </div>  
                    </div>  
                  </div>  
                </form>  
                <div class="modal-footer">  
                  <button class="btn btn-success" type="submit">Save</button>  
                  <button class="btn btn-warning" type="reset">Reset</button>  
                  <button class="btn btn-danger" data-dismiss="modal">Cancel</button>  
                </div>  
              </div>  
            </div>  
          </div>  
  
</body>  
</html>