函数嵌套函数,内部函数能够引用外部函数的参数和变量,参数和变量不会被垃圾回收机制收回.javascript
<script type="text/javascript"> window.onload = function(){ var aLi = document.getElementsByTagName('li'); for(var i=0;i<aLi.length;i++) { (function(i){ aLi[i].onclick = function(){ alert(i); } })(i); } } </script> ...... <ul> <li>111</li> <li>222</li> <li>333</li> <li>444</li> <li>555</li> </ul>
<script type="text/javascript"> var count = (function(){ var a = 0; function add(){ a++; return a; } return add; })() count(); count(); var nowcount = count(); alert(nowcount); </script>
1.documentcss
document.referrer //获取上一个跳转页面的地址(须要服务器环境)html
2.locationjava
window.location.href //获取或者重定url地址 window.location.search //获取地址参数部分 window.location.hash //获取页面锚点或者叫哈希值jquery
3.Math程序员
Math.random 获取0-1的随机数 Math.floor 向下取整 Math.ceil 向上取整编程
一、面向过程:全部的工做都是现写现用。
二、面向对象:是一种编程思想,许多功能事先已经编写好了,在使用时,只须要关注功能的运用,而不须要这个功能的具体实现过程。数组
将相关的变量和函数组合成一个总体,这个总体叫作对象,对象中的变量叫作属性,变量中的函数叫作方法。javascript中的对象相似字典。服务器
1.单体:闭包
<script type="text/javascript"> var Tom = { name : 'tom', age : 18, showname : function(){ alert('个人名字叫'+this.name); }, showage : function(){ alert('我今年'+this.age+'岁'); } } </script>
2.工厂模式:
<script type="text/javascript"> function Person(name,age,job){ var o = new Object(); 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.showname(); </script>
3.构造函数:
<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); }; } var tom = new Person('tom',18,'程序员'); var jack = new Person('jack',19,'销售'); alert(tom.showjob==jack.showjob); </script>
4.原型模式:
<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); }; var tom = new Person('tom',18,'程序员'); var jack = new Person('jack',19,'销售'); alert(tom.showjob==jack.showjob); </script>
5.继承:
<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) { fclass.call(this,name,age); this.job = job; } sclass.prototype = new fclass(); sclass.prototype.showjob = function(){ alert(this.job); } var tom = new sclass('tom',19,'全栈工程师'); tom.showname(); tom.showage(); tom.showjob(); </script>
jQuery是目前使用最普遍的javascript函数库。据统计,全世界排名前100万的网站,有46%使用jQuery,远远超过其余库。微软公司甚至把jQuery做为他们的官方库。
<script type="text/javascript" src="js/jquery-1.12.2.js"></script>
将获取元素的语句写到页面头部,会由于元素尚未加载而出错,jquery提供了ready方法解决这个问题,它的速度比原生的 window.onload 更快。
<script type="text/javascript"> $(function(){ 代码 }); </script>
选择某个网页元素,而后对它进行某种操做
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元素
$('#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的元素
同一个函数完成取值和赋值
// 获取div的样式 $("div").css("width"); $("div").css("color"); //设置div的样式 $("div").css("width","30px"); $("div").css("height","30px"); $("div").css({fontSize:"30px",color:"red"});
选择器获取的多个元素,获取信息获取的是第一个,好比:$("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样式
// 取出html内容 var $htm = $('#div1').html(); // 设置html内容 $('#div1').html('<span>添加文字</span>');
// 取出文本内容 var $htm = $('#div1').text(); // 设置文本内容 $('#div1').text('<span>添加文字</span>');
// 取出图片的地址 var $src = $('#img1').attr('src'); // 设置图片的地址和alt属性 $('#img1').attr({ src: "test.jpg", alt: "Test Image" });
给元素绑定click事件,能够用以下方法:
$('#btn1').click(function(){ // 内部的this指的是原生对象 // 使用jquery对象用 $(this) }) $(function(){ // 给按钮绑定click事件 $('#btn').click(function(){ //重复切换sty样式 $('.box').toggleClass('sty'); }) })