jQueryとBootstrapを使って簡単にモーダルウィンドウを表示
jQueryを使用してBootstrapモーダルウィンドウを開く方法
手順
- HTML
まず、モーダルウィンドウ用のHTMLコードを用意します。
<button type="button" class="btn btn-primary" id="open-modal">モーダルウィンドウを開く</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">モーダルウィンドウタイトル</h4>
</div>
<div class="modal-body">
ここにモーダルウィンドウの内容を記述します。
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">閉じる</button>
<button type="button" class="btn btn-primary">送信</button>
</div>
</div>
</div>
</div>
上記コードでは、モーダルウィンドウを開くボタンと、モーダルウィンドウ本体を用意しています。
- jQuery
次に、jQueryを使用してモーダルウィンドウを開くコードを記述します。
$(document).ready(function() {
$("#open-modal").click(function() {
$("#myModal").modal("show");
});
});
上記コードでは、$("#open-modal").click(function() {
の部分で、モーダルウィンドウを開くボタンがクリックされた時に処理を実行するようにしています。処理内容は、$("#myModal").modal("show");
で、モーダルウィンドウ #myModal
を表示します。
ポイント
- モーダルウィンドウを開くボタンには、
data-toggle="modal"
とdata-target="#myModal"
という属性を指定する必要があります。 #myModal
は、モーダルウィンドウのIDです。必要に応じて変更してください。- モーダルウィンドウの内容は、自由にカスタマイズできます。
上記のコードを参考に、jQueryを使用してBootstrapモーダルウィンドウを開いてみてください。
- 上記のコードは、Bootstrap 4.0 を使用しています。Bootstrap 3.x を使用している場合は、コードを少し変更する必要があります。
- モーダルウィンドウを閉じるには、
data-dismiss="modal"
属性を持つ要素をクリックします。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Bootstrapモーダルウィンドウサンプル</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>
<button type="button" class="btn btn-primary" id="open-modal">モーダルウィンドウを開く</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">モーダルウィンドウタイトル</h4>
</div>
<div class="modal-body">
ここにモーダルウィンドウの内容を記述します。
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">閉じる</button>
<button type="button" class="btn btn-primary">送信</button>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$("#open-modal").click(function() {
$("#myModal").modal("show");
});
});
</script>
</body>
</html>
コード解説
- 上記コードは、Bootstrap 4.0 と jQuery を使用しています。
index.html
ファイルには、HTMLコードと JavaScript コードが含まれています。bootstrap.min.css
、jquery.min.js
、popper.min.js
、bootstrap.min.js
ファイルは、Bootstrap 4.0 の必要なファイルです。#open-modal
は、モーダルウィンドウを開くボタンのIDです。$("#open-modal").click(function() {
の部分で、モーダルウィンドウを開くボタンがクリックされた時に処理を実行するようにしています。$("#myModal").modal("show");
で、モーダルウィンドウ#myModal
を表示します。
動作確認
上記コードを保存して、ブラウザで開くと、モーダルウィンドウが開くボタンが表示されます。ボタンをクリックすると、モーダルウィンドウが表示されます。
jQueryを使用してBootstrapモーダルウィンドウを開くその他の方法
data-toggle="modal" 属性を使用する
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">モーダルウィンドウを開く</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
...
</div>
この方法は、JavaScriptコードを書く必要がないため、簡単です。
$.modal()
メソッドを使用して、モーダルウィンドウを開くことができます。
$(document).ready(function() {
$.modal('#myModal');
});
この方法は、モーダルウィンドウを開くタイミングを自由に制御したい場合に便利です。
$(document).ready(function() {
$('#myModal').modal('show');
});
javascript jquery twitter-bootstrap