day-16 jquery的DOM文档操作及bootstrap
1. jquery的DOM文档操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div class="box">
<p>黄金爱家</p>
</div>
<button>按钮</button>
<!--<p>alex</p>-->
<script src="jquery-3.3.1.js"></script>
<script>
$(function () {
var imgSrc = './timg.jpg';
var alt = '美女';
var aHref = 'http://www.baidu.com'
// 父.append(子) 子元素 可以是 string js对象 jquery对象
// $('.box').append('<h2>太白结婚了</h2>');
// $('.box').append(` <ul>
// <li>
// <a href=${aHref}>
// <img src=${imgSrc} alt=${alt}>
// </a>
// </li>
// </ul>`);
// var oH3 = document.createElement('h3');
// oH3.innerText = 'taibai';
// console.log( $('.box'));
// $('.box').append(oH3);
//相当于一个移动操作
// $('.box').append($('p'));
// 追加到
// $(子).appendTo(父)
// $('p').click()
// var oDiv = document.getElementsByClassName('box')[0];
// // $('<h3>哈哈哈哈</h3>').appendTo($('.box'));
// // $('<h3>哈哈哈哈</h3>').appendTo('.box');
// $('<h3>哈哈哈哈</h3>').appendTo(oDiv).click(function () {
// alert(1);
// });
// 追加到父元素中的第一个元素
// $('.box').prepend('哈哈哈')
// $('.box').prepend('<h5>哈哈哈2</h5>')
// 兄弟之间插入
$('p').after('<h3>alex</h3>');
$('<h3>女神</h3>').insertAfter('p');
$('p').replaceWith('结婚了');
$('<h5>哈哈哈</h5>').replaceAll('h3');
$('button').click(function () {
alert(1);
// 删除节点 事件也删除
// var aBtn = $('button').remove();
// 删除节点 事件保留
// var aBtn = $('button').detach();
// console.log(aBtn);
//
// $('.box').prepend(aBtn);
$('.box').empty();
// 1. 初始化的时候,有初始化 渲染开销 对于 文档 DOM 如果是频繁性的切换 建议使用 display:block|none addClass
//2. 少量的切换 建议使用 创建元素 删除元素 性能消耗 创建==》销毁
});
});
</script>
</body>
</html>
2.事件对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box{
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div class="box">
<a href="http://www.baidu.com">按钮</a>
</div>
<form action="http://www.baidu.com/s">
<input type="text" name = 'wd' id="wd">
<input type="submit" id="submit">
</form>
<script src="jquery-3.3.1.js"></script>
<script>
$(function () {
$('a').click(function (event) {
//a form event.preventDefault();阻止默认事件
event.preventDefault();
//阻止冒泡
event.stopPropagation()
console.log('a点击了');
// 阻止冒泡
// event.stopPropagation()
// window.location.href
});
$('#submit,#wd').click(function () {
event.stopPropagation();
//同时阻止了默认事件和冒泡
// return false;
});
$('form').submit(function (event) {
event.preventDefault();
event.stopPropagation()
console.log(1111);
// ajax请求
$.ajax({
url:'http://www.baidu.com/s',
type:'get',
success:function (data) {
console.log(data);
}
});
});
$('.box').click(function () {
console.log('盒子被点击了')
})
$('body').click(function () {
console.log('body被点击了')
})
$('html').click(function () {
console.log('html被点击了')
})
});
</script>
</body>
</html>
3.jquery的位置信息
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
padding: 0;
margin: 0;
}
.box{
background-color: red;
padding: 10px;
border: 1px solid yellow;
/*margin-top: 100px;*/
/*margin-left: 100px;*/
position: relative;
top: 50px;
}
</style>
</head>
<body style="height: 2000px">
<div class="box">alex</div>
<script src="jquery-3.3.1.js"></script>
<script>
$(function () {
$('.box').width(200).height(300);
// innerWidth innerHeight 内部宽和高 不包含border
// console.log($('.box').innerWidth());
// $('.box').innerWidth(400)
// outerHeight outerWidth 外部宽和高 包含border
// console.log( $('.box').outerWidth());
// console.log( $('.box').offset().top);
$(document).scroll(function () {
// console.log(22222);
console.log( $(document).scrollTop())
})
})
</script>
</body>
</html>
4. 滚动固定导航栏
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
padding: 0;
margin: 0;
}
.header{
width: 100%;
height: 40px;
background-color: rgba(0,0,0,.3);
}
.header_box{
position: fixed;
width: 100%;
height: 200px;
background-color: #555;
left: 0;
top: 0;
z-index: 888;
}
.fix_header{
position: fixed;
width: 100%;
height: 80px;
background-color: red;
top: 0;
left: 0;
display: none;
}
.content{
width: 500px;
height: 300px;
background-color: yellow;
position: absolute;
left: 50%;
margin-left: -250px;
top: 300px;
}
</style>
</head>
<body style="height: 2000px">
<div class="header">
<a href="javascript:void(0)" id="changeFu">换肤</a>
<div class="header_box" style="display: none;">
<a href="javascript:void(0)" class="hotmen">热门</a>
<a href="javascript:void(0)" class="slideUp">收起</a>
</div>
</div>
<div class="content">
</div>
<div class="fix_header">
固定导航栏
</div>
<script src="jquery-3.3.1.js"></script>
<script>
$(function () {
$('#changeFu').click(function (e) {
e.stopPropagation();
$('.header_box').stop().slideDown(1000);
});
$('.hotmen').click(function (e) {
e.stopPropagation()
console.log('点了hotmen');
});
$('.slideUp').click(function (e) {
e.stopPropagation();
$('.header_box').stop().slideUp(1000);
})
$('.header_box,.header').click(function () {
return false;
})
$('body').click(function () {
alert(1);
$('.header_box').stop().slideUp(1000);
});
$(document).scroll(function () {
//获取黄色的盒子到顶部的距离
var top = $('.content').offset().top;
var docScrollTop = $(document).scrollTop()
if (docScrollTop > top){
$('.fix_header').show();
}else {
$('.fix_header').hide();
}
});
})
</script>
</body>
</html>
5.使用Bootstrap
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>Bootstrap 101 Template</title>
<!-- Bootstrap 全局的css-->
<link href="./bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
<!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
<!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
<!--[if lt IE 9]>
<script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">路飞学诚</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">首页 <span class="sr-only">(current)</span></a></li>
<li><a href="#">学位</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-right">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">注册</button>
<button type="submit" class="btn btn-success">登录</button>
</form>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<div class="container">
<div class="row">
<div class="col-lg-3 col-md-4 col-sm-6">
Bootstrap 提供了一套响应式、移动设备优先的流式栅格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列。它包含了易于使用的预定义类,还有强大的mixin 用于生成更具语义的布局。
</div>
<div class="col-lg-3 col-md-4 col-sm-6">
Bootstrap 提供了一套响应式、移动设备优先的流式栅格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列。它包含了易于使用的预定义类,还有强大的mixin 用于生成更具语义的布局。
</div>
<div class="col-lg-3 col-md-4 col-sm-6">
Bootstrap 提供了一套响应式、移动设备优先的流式栅格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列。它包含了易于使用的预定义类,还有强大的mixin 用于生成更具语义的布局。
</div>
<div class="col-lg-3 col-md-4 col-sm-6">
Bootstrap 提供了一套响应式、移动设备优先的流式栅格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列。它包含了易于使用的预定义类,还有强大的mixin 用于生成更具语义的布局。
</div>
</div>
</div>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
6. 全局的css样式
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>Bootstrap 101 Template</title>
<!-- Bootstrap 全局的css-->
<link href="./bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
<!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
<!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
<!--[if lt IE 9]>
<script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">路飞学诚</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">首页 <span class="sr-only">(current)</span></a></li>
<li><a href="#">学位</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-right">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">注册</button>
<button type="submit" class="btn btn-success">登录</button>
</form>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<div class="container">
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">表格</h3>
</div>
<div class="panel-body">
<table class="table table-striped table-bordered table-hover table-condensed">
<tr>
<td>id</td>
<td>name</td>
<td>age</td>
</tr>
<tr>
<td>1</td>
<td class="active">alex</td>
<td class="info">198</td>
</tr>
<tr>
<td class="success">2</td>
<td class="danger">tailaing</td>
<td class="warning">200</td>
</tr>
</table>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">表单</h3>
</div>
<div class="panel-body">
<form>
<div class="form-group">
<label for="user">用户名</label>
<input type="text" class="form-control" id="user" placeholder="请输入用户名">
</div>
<div class="form-group">
<label for="password">密码</label>
<input type="password" class="form-control" id="password"
placeholder="请输入密码">
</div>
<button type="submit" class="btn btn-default">登录</button>
<button type="submit" class="btn btn-success">登录</button>
<button type="submit" class="btn btn-danger">登录</button>
<button type="submit" class="btn btn-info">登录</button>
<button type="submit" class="btn btn-warning">登录</button>
<a type="submit" class="btn btn-primary btn-lg">登录</a>
</form>
<form class="form-inline">
<div class="form-group">
<label class="sr-only" for="exampleInputAmount">Amount (in dollars)</label>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-subscript"></span>
</div>
<input type="text" class="form-control" id="exampleInputAmount" placeholder="Amount">
<div class="input-group-addon">.00</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Transfer cash</button>
<button type="button" class="close" aria-label="Close"><span aria-hidden="true">×</span></button>
<div class="show">111111111</div>
</form>
</div>
</div>
</div>
</div>
</div>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>