闭包和jQuery基本语法
2018-09-19 本文已影响0人
心软脾气硬01
闭包
1、闭包的用途:存循环的索引值
函数嵌套函数,内部函数可以引用外部函数的参数和变量,参数和变量不会被垃圾回收机制收回
<style type="text/css">
li{
height: 30px;
background-color: gold;
margin-bottom: 10px;
}
</style>
<script type="text/javascript">
//闭包的用途:存循环的索引值
window.onload = function(){
var aLi = document.getElementsByTagName('li');
// alert(aLi.length);//8
for(var i=0; i<aLi.length; i++){
/*
aLi[i].onclick = function(){
alert(i);//每个li都弹出8,因为点击时循环已完毕,i最后为8
}
*/
(function(k){//这里的k是形参
aLi[k].onclick = function(){
alert(k);//弹出每个li的索引值
}
})(i);//这里的i是实参
}
}
</script>
2、闭包做私有变量计数器
每调用一次count,a就自增一次
alert(count());//1
alert(count());//2
var c = count();
alert(c);//3
3、闭包做选项卡
<script type="text/javascript">
//闭包做选项卡
window.onload = function(){
var aBtn = document.getElementById('btns').getElementsByTagName('input');
var aCon = document.getElementById('contents').getElementsByTagName('div');
// alert(aCon.length);
//循环所有的选项卡按钮
for(var i=0; i<aBtn.length; i++){
(function(i){
//给每个选项卡按钮添加点击事件
aBtn[i].onclick = function(){
//遍历所有选项卡按钮
for(var j=0; j<aBtn.length; j++){
//将每个选项卡按钮都设为灰色
aBtn[j].className = '';
//将每个内容区都隐藏
aCon[j].className = '';
}
//this代表当前点击的Button对象
this.className = 'cur';//当前点击的按钮为金色
// alert(i);//不加闭包时,不管点哪个按钮,i都等于3
//加闭包保存了索引值才有效
aCon[i].className = 'active';//当前点击的按钮对应的内容显示
}
})(i);
}
}
</script>
获取地址栏参数
window.onload = function(){
//url?aa=tom#12
var data = window.location.search;//?aa=tom
var hash = window.location.hash;//#12
alert(hash);//#12
var oSpan = document.getElementById('span01');
// alert(data);//?aa=tom
var arr = data.split('=');
// alert(arr);//?aa,tom
var name = arr[1];
oSpan.innerHTML = name;
}
math弹出随机数以及取整
<script type="text/javascript">
// var num = Math.random();
// alert(num);//弹出0-1之间的随机数
var a = 10;
var b = 20;
// var num = Math.random()*(b-a)+a;
// alert(num);//弹出10-20之间的随机数
var arr = [];
for(var i=0; i<20; i++){
// var num = Math.floor(Math.random()*(b-a)+a);//向下取整,10-19
var num = Math.floor(Math.random()*(b-a + 1)+a);//向下取整,10-20
arr.push(num);//生成一个数就放进数组
}alert(arr);//17,20,20,11,11,19,17,16,10,11,16,11,18,13,13,11,17,14,19,19
</script>
创建对象
1、单体创建对象
<script type="text/javascript">
var Tom = {
// 属性
name:'tom',
age:18,
// 方法
showName:function(){
alert(this.name);
},
showAge:function(){
alert(this.age);
}
}
//调用属性
alert(Tom.name);
alert(Tom.age);
//调用方法
Tom.showName();
</script>
2、工厂模式创建对象
<script type="text/javascript">
function Person(name,age,job){
//创建一个空对象
// var o = new Object();//方式一
var o = {};//方式二
o.name = name;
o.age = age;
o.job = job;
o.showName = function(){
alert(this.name);
}
o.showAge = function(){
alert(this.age);
}
o.showJob = function(){
alert(this.job);
}
return o;
}
var Tom = Person('tom',18,'程序猿');
Tom.showJob();
var Jack = Person('jack',19,'攻城狮');
Jack.showJob();
</script>
构造函数
1、构造函数
<script type="text/javascript">
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
this.showName = function(){
alert(this.name);
}
this.showAge = function(){
alert(this.age);
}
this.showJob = function(){
alert(this.job);
}
}
//new的作用就相当于工厂模式中最开始创建了一个空对象,最后把对象返回
var Bob = new Person('bob',18,'产品汪');
Bob.showJob();
var Alex = new Person('alex',19,'运营喵');
Alex.showJob();
alert(Bob.showName == Alex.showName);//false
</script>
2、原型模式
<script type="text/javascript">
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
Person.prototype.showName = function(){
alert(this.name);
}
Person.prototype.showAge = function(){
alert(this.age);
}
Person.prototype.showJob = function(){
alert(this.job);
}
}
//先去自己的对象中找showName函数,再去构造函数的原型找
var Lucy = new Person('lucy',18,'测试鼠');
//重写自身对象中的方法,不会影响其它对象
Lucy.showName = function(){
alert('我的名字是' + this.name);
}
Lucy.showName();//我的名字是lucy
var Lily = new Person('lily',19,'市场鸡');
Lily.showName();//lily
alert(Lucy.showName == Lily.showName);//false
</script>
3、call和apply的区别
二者都可以改变当前的this,区别在于apply方法要将参数放入数组中再传参
4、函数的继承
<script type="text/javascript">
//父类
function Fclass(name, age){
this.name = name;
this.age = age;
}
Fclass.prototype.showName = function(){
alert(this.name);
}
Fclass.prototype.showAge = function(){
alert(this.age);
}
//子类
function Sclass(name, age, job){
//属性用call或者apply的方式来继承
Fclass.call(this, name, age);
this.job = job;
}
//方法继承:将父类的一个实例赋值给子类的原型属性
Sclass.prototype = new Fclass();
Sclass.prototype.showJob = function(){
alert(this.job);
}
//由于已经继承了父类的属性和方法,所以可以直接调用
var Driver = new Sclass('tom',18,'老司机');
Driver.showName();
Driver.showAge();
Driver.showJob();
</script>
5、新增选择器
<script type="text/javascript">
window.onload = function(){
var oDiv = document.querySelector('#div1');
alert(oDiv);//弹出[object HTMLDivElement],表示选择了该Div
//如果要选择多个元素用querySelectorAll
var aLi = document.querySelectorAll('.list li');
alert(aLi.length);//8
}
</script>
</head>
<body>
<div id="div1">这是一个div元素</div>
<ul class="list">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
</ul>
</body>
</html>
jQuery
1、jQuery加载
JS写法
window.onload = function(){
var oDiv = document.getElementById('div');
alert(oDiv.innerHTML);//这是一个div元素
}
Query的完整写法
比上面JS写法先弹出,因为window.onload是把页面元素加载、渲染完才弹出,而ready()是把所有页面的节点加载完之后就弹出了,不用等渲染了
$(document).ready(function(){
var $div = $('#div');
alert('jQuery:' + $div.html());//jQuery:这是一个div元素
})
简写方式
$(function(){
var $div = $('#div');//CSS样式怎么写,这里就怎么写
//html()方法相当于原生JS的innerHTML
alert($div.html() + 'jQuery');
})
2、jQuery选择器
jquery用法思想一
选择某个网页元素,然后对它进行某种操作
jquery选择器
jquery选择器可以快速地选择元素,选择规则和css样式相同,使用length属性判断是否选择成功。
$(document) //选择整个文档对象
$('li') //选择所有的li元素
$('#myId') //选择id为myId的网页元素
$('.myClass') // 选择class为myClass的元素
$('input[name=first]') // 选择name属性等于first的input元素
$('#ul1 li span') //选择id为为ul1元素下的所有li下的span元素
对选择集进行修饰过滤(类似CSS伪类)
$('#ul1 li:first') //选择id为ul1元素下的第一个li
$('#ul1 li:odd') //选择id为ul1元素下的li的奇数行
$('#ul1 li:eq(2)') //选择id为ul1元素下的第3个li
$('#ul1 li:gt(2)') // 选择id为ul1元素下的前三个之后的li
$('#myForm :input') // 选择表单中的input元素
$('div:visible') //选择可见的div元素
对选择集进行函数过滤
$('div').has('p'); // 选择包含p元素的div元素
$('div').not('.myClass'); //选择class不等于myClass的div元素
$('div').filter('.myClass'); //选择class等于myClass的div元素
$('div').first(); //选择第1个div元素
$('div').eq(5); //选择第6个div元素
选择集转移
$('div').prev('p'); //选择div元素前面的第一个p元素
$('div').next('p'); //选择div元素后面的第一个p元素
$('div').closest('form'); //选择离div最近的那个form父元素
$('div').parent(); //选择div的父元素
$('div').children(); //选择div的所有子元素
$('div').siblings(); //选择div的同级元素
$('div').find('.myClass'); //选择div内的class等于myClass的元素
<script type="text/javascript">
$(function(){
//选择元素的规则和css样式相同
$('#div1').css({color: 'pink'});
$('.box').css({fontSize: '30px'});
$('.list li').css({
background: 'green',
color: '#fff',
fontSize: '20px',
marginBottom: '10px'
});
})
</script>
3、选择集转移
<script type="text/javascript">
$(function(){
//prev()是同级的上一个元素,prevAll()是同级的上面所有的元素
//next()是同级的下一个元素,nextAll()是同级的下面所有的元素
//修改#div1的下一个元素的样式
$('#div1').next().css({color: 'red'});
//修改#div1的下面所有p标签设置样式
$('#div1').nextAll('p').css({color: 'red'});
//选择上一级的父元素
/*$('#span01').parent().css({
width:'100px',
height:'100px',
background:'gold'
})*/
//获取祖级用$('#span02').parent().parent()不可取,可用closest('div')获取离span02最近的div
//closest可以选择离自己最近的元素,元素可以是父级,也可以是子集
$('#span01').closest('div').css({
width:'200px',
height:'200px',
background:'pink'
})
/*
$('.list li')与$('.list').children()的区别:
原始的选择集不一样
$('.list li')不能通过end()回到父级
$('.list').children()可以通过end()回到父级
*/
$('.list').children().css({
background:'gold',
height:'30px',
marginBottom:'10px'
}).end().css({
background:'green'
})
//eq(2)是选择索引等于2的第三个li,siblings()表示除第三个之外的其它兄弟li
$('.list2 li:eq(2)').css({background:'gold'}).siblings().css({background:'green'});
//find()是选择div内的class等于link1的元素
$('#div2').find('.link1').css({color:'red'});
})
</script>
4、jQuery操作样式
jQuery用同一个函数即可以取值、也可以赋值
读取样式
选择器获取的多个元素,获取信息获取的是第一个,比如:$("div").css("width"),获取的是第一个div的width。
操作样式类名
$("#div1").addClass("divClass2") //为id为div1的对象追加样式divClass2
$("#div1").removeClass("divClass") //移除id为div1的对象的class名为divClass的样式
$("#div1").removeClass("divClass divClass2") //移除多个样式
$("#div1").toggleClass("anotherClass") //重复切换anotherClass样式
alert($('#div1').css('fontSize'));//16px
//设置(写入)样式
$('#div1').css({background:'gold'});
//增加行间样式
$('#div1').addClass('big');
//减少行间样式,多个样式用空格分开
$('#div1').removeClass('div2 big');
5、给元素绑定click事件,可以用如下方法:
$('#btn1').click(function(){
// 内部的this指的是原生对象
// 使用jquery对象用 $(this)
})
6、jQuery索引值
<script type="text/javascript">
$(function(){
$('.list li').click(function(){
// alert(this.innerHTML);//弹出标签中的内容
alert($(this).index());//弹出下标
})
})
</script>
7、jQuery做选项卡
<style type="text/css">
.btns{
width: 500px;
height: 50px;
}
/*选项卡的样式*/
.btns input{
width: 100px;
height: 50px;
background-color: #ddd;/*默认灰色*/
color: #666;
border: 0px;
}
/*被选中的选项卡的样式*/
.btns input.cur{
background-color: gold;
}
/*内容区的样式*/
.contents div{
width: 500px;
height: 300px;
background-color: gold;
display: none;/*默认隐藏*/
line-height: 300px;
text-align: center;
}
/*被选中的内容区的样式*/
.contents div.active{
display: block;
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
$('#btns input').click(function() {
//失去焦点,避免出现默认的蓝框
$(this).blur();
//this是原生的对象
// alert(this);//弹出[object HTMLInputElement],说明this就是当前点击的input元素
//jQuery的this对象使用时要用$()包起来,这样就可以调用jQuery的方法了
//给当前元素添加选中样式,为兄弟元素移除选中样式
$(this).addClass('cur').siblings().removeClass('cur');
//$(this).index()获取当前按钮所在层级范围的索引值
//显示对应索引的内容区,隐藏其它兄弟内容区
$('#contents div').eq($(this).index()).addClass('active').siblings().removeClass('active');
});
})
</script>
</head>
<body>
<div class="btns" id="btns">
<input type="button" value="tab01" class="cur">
<input type="button" value="tab02">
<input type="button" value="tab03">
</div>
<div class="contents" id="contents">
<div class="active">tab文字内容一</div>
<div>tab文字内容二</div>
<div>tab文字内容三</div>
</div>
</body>