上一章博客我们讲解了Bootstrap的组件
这一章我们来讲一下一些JavaScript组件
data属性有很多,这些属性都是在data后紧跟一些名词,例如比较常用的data-toggle、data-target、data-dismiss等
考虑到使用的方便性和快捷性,比较推荐使用data属性来调用JavaScript插件
语法
$(document).off('.data-api');
语法
// 关闭旋转轮播插件的data属性
$(document).off('.carousel.data-api');
如果使用压缩版的 bootstrap.min.js 文件,则无需再单独引入过渡插件,因为此时过渡插件的功能已经融入到其他插件中
$.support.transition = false;
示例
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>模态框title>
<link rel="stylesheet" href="../css/bootstrap.min.css">
head>
<style>
style>
<body>
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
显示模态框
button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<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">Modal titleh4>
div>
<div class="modal-body">
<form class="form-inline" action="#">
<div class="form-group">
<label for="user">用户名:label>
<input type="text" class="form-control" id="user" placeholder="请输入用户名">
div>
<div class="form-group">
<label for="pwd">密码:label>
<input type="password" class="form-control" id="pwd" placeholder="请输入密码">
div>
<input type="submit" class="form-control" value="登录">
form>
div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Closebutton>
<button type="button" class="btn btn-primary">Save changesbutton>
div>
div>
div>
div>
body>
<script src="../js/jquery.js">script>
<script src="../js/bootstrap.min.js">script>
<script>
script>
html>
data-target="#myModal" 里面的 #myModal 要和 下面的id对应
除data属性之外,也可以使用JavaScript操作模态框
只需一行 JavaScript 代码
JavaScript操作模态框
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>模态框title>
<link rel="stylesheet" href="../css/bootstrap.min.css">
head>
<style>
style>
<body>
<button type="button" class="btn btn-primary btn-lg" id="jsbtn">
js模态框
button><br>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<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">Modal titleh4>
div>
<div class="modal-body">
<form class="form-inline" action="#">
<div class="form-group">
<label for="user">用户名:label>
<input type="text" class="form-control" id="user" placeholder="请输入用户名">
div>
<div class="form-group">
<label for="pwd">密码:label>
<input type="password" class="form-control" id="pwd" placeholder="请输入密码">
div>
<input type="submit" class="form-control" value="登录">
form>
div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Closebutton>
<button type="button" class="btn btn-primary">Save changesbutton>
div>
div>
div>
div>
body>
<script src="../js/jquery.js">script>
<script src="../js/bootstrap.min.js">script>
<script>
$("#jsbtn").click(function(){
$('#myModal').modal('show')
})
script>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>标签页title>
<link rel="stylesheet" href="../css/bootstrap.min.css">
head>
<body>
<ul id="myTab" class="nav nav-tabs">
<li class="active">
<a href="#home" data-toggle="tab">
菜鸟教程
a>
li>
<li><a href="#ios" data-toggle="tab">iOSa>li>
<li class="dropdown">
<a href="#" id="myTabDrop1" class="dropdown-toggle"
data-toggle="dropdown">Java
<b class="caret">b>
a>
<ul class="dropdown-menu" role="menu" aria-labelledby="myTabDrop1">
<li><a href="#jmeter" tabindex="-1" data-toggle="tab">jmetera>li>
<li><a href="#ejb" tabindex="-1" data-toggle="tab">ejba>li>
ul>
li>
ul>
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade in active" id="home">
<p>菜鸟教程是一个提供最新的web技术站点,本站免费提供了建站相关的技术文档,帮助广大web技术爱好者快速入门并建立自己的网站。菜鸟先飞早入行——学的不仅是技术,更是梦想。p>
div>
<div class="tab-pane fade" id="ios">
<p>iOS 是一个由苹果公司开发和发布的手机操作系统。最初是于 2007 年首次发布 iPhone、iPod Touch 和 Apple
TV。iOS 派生自 OS X,它们共享 Darwin 基础。OS X 操作系统是用在苹果电脑上,iOS 是苹果的移动版本。p>
div>
<div class="tab-pane fade" id="jmeter">
<p>jMeter 是一款开源的测试软件。它是 100% 纯 Java 应用程序,用于负载和性能测试。p>
div>
<div class="tab-pane fade" id="ejb">
<p>Enterprise Java Beans(EJB)是一个创建高度可扩展性和强大企业级应用程序的开发架构,部署在兼容应用程序服务器(比如 JBOSS、Web Logic 等)的 J2EE 上。
p>
div>
div>
body>
<script src="../js/jquery.js">script>
<script src="../js/bootstrap.min.js">script>
<script>
$("ul a").on("shown.bs.tab",function(e){
e.target
e.relatedTarget
})
script>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>标签页title>
<link rel="stylesheet" href="../css/bootstrap.min.css">
head>
<body>
<ul id="myTab" class="nav nav-pills">
<li class="active">
<a href="#home" data-toggle="pill">
菜鸟教程
a>
li>
<li><a href="#ios" data-toggle="pill">iOSa>li>
<li class="dropdown">
<a href="#" id="myTabDrop1" class="dropdown-toggle"
data-toggle="dropdown">Java
<b class="caret">b>
a>
<ul class="dropdown-menu" role="menu" aria-labelledby="myTabDrop1">
<li><a href="#jmeter" tabindex="-1" data-toggle="pill">jmetera>li>
<li><a href="#ejb" tabindex="-1" data-toggle="pill">ejba>li>
ul>
li>
ul>
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade in active" id="home">
<p>菜鸟教程是一个提供最新的web技术站点,本站免费提供了建站相关的技术文档,帮助广大web技术爱好者快速入门并建立自己的网站。菜鸟先飞早入行——学的不仅是技术,更是梦想。p>
div>
<div class="tab-pane fade" id="ios">
<p>iOS 是一个由苹果公司开发和发布的手机操作系统。最初是于 2007 年首次发布 iPhone、iPod Touch 和 Apple
TV。iOS 派生自 OS X,它们共享 Darwin 基础。OS X 操作系统是用在苹果电脑上,iOS 是苹果的移动版本。p>
div>
<div class="tab-pane fade" id="jmeter">
<p>jMeter 是一款开源的测试软件。它是 100% 纯 Java 应用程序,用于负载和性能测试。p>
div>
<div class="tab-pane fade" id="ejb">
<p>Enterprise Java Beans(EJB)是一个创建高度可扩展性和强大企业级应用程序的开发架构,部署在兼容应用程序服务器(比如 JBOSS、Web Logic 等)的 J2EE 上。
p>
div>
div>
body>
<script src="../js/jquery.js">script>
<script src="../js/bootstrap.min.js">script>
<script>
$("ul a").on("shown.bs.tab",function(e){
e.target
e.relatedTarget
})
script>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<link rel="stylesheet" href="../css/bootstrap.min.css">
head>
<style>
.focus {
width: 500px;
height: 500px;
background-color: pink;
margin: 100px auto;
}
.carousel,
.carousel img {
width: 100%;
height: 500px!important;
}
style>
<body>
<div class="container">
div>
<hr>
<div class="focus">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active">li>
<li data-target="#carousel-example-generic" data-slide-to="1">li>
<li data-target="#carousel-example-generic" data-slide-to="2">li>
ol>
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="../../淘宝/img/轮播图1.png" alt="...">
div>
<div class="item">
<img src="../../淘宝/img/轮播图2.png" alt="...">
div>
<div class="item">
<img src="../../淘宝/img/轮播图3.png" alt="...">
div>
div>
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true">span>
<span class="sr-only">Previousspan>
a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true">span>
<span class="sr-only">Nextspan>
a>
div>
div>
body>
<script src="../js/jquery.js">script>
<script src="../js/bootstrap.min.js">script>
<script>
$('.carousel').carousel({
interval: 2000
})
script>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<link rel="stylesheet" href="../css/bootstrap.min.css">
head>
<style>
.focus {
width: 500px;
height: 500px;
background-color: pink;
margin: 100px auto;
}
.carousel,
.carousel img {
width: 100%;
height: 500px!important;
}
style>
<body>
<div class="container">
div>
<hr>
<div class="focus">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active">li>
<li data-target="#carousel-example-generic" data-slide-to="1">li>
<li data-target="#carousel-example-generic" data-slide-to="2">li>
ol>
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="../../淘宝/img/轮播图1.png" alt="...">
<div class="carousel-caption">
<h3>途欢黄金体验h3>
<p>人生路途与健康同行畅快出行p>
div>
div>
<div class="item">
<img src="../../淘宝/img/轮播图2.png" alt="...">
div>
<div class="item">
<img src="../../淘宝/img/轮播图3.png" alt="...">
div>
div>
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true">span>
<span class="sr-only">Previousspan>
a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true">span>
<span class="sr-only">Nextspan>
a>
div>
div>
body>
<script src="../js/jquery.js">script>
<script src="../js/bootstrap.min.js">script>
<script>
$('.carousel').carousel({
interval: 2000
})
script>
html>
送大家一句话:只要有决心和毅力,什么时候也不算晚。