2019-06-13

2019-06-13  本文已影响0人  々_18C

面向过程与面向对象编程

1、面向过程:所有的工作都是现写现用。

2、面向对象:是一种编程思想,许多功能事先已经编写好了,在使用时,只需要关注功能的运用,而不需要这个功能的具体实现过程。

javascript对象

将相关的变量和函数组合成一个整体,这个整体叫做对象,对象中的变量叫做属性,变量中的函数叫做方法。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>

2、构造函数 

<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>

3、原型模式 

<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>

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)

        {

            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>

new的作用就相当于工厂模式中最开始创建了一个空对象,最后把对象返回

call和apply的区别

二者都可以改变当前的this,区别在于apply方法要将参数放入数组中再传参

属性用call或者apply的方式来继承

方法继承:将父类的一个实例赋值给子类的原型属性

新选择器

1、document.querySlector()          弹出一个元素

2、document.querySlectorAll()     选择多个元素

jquery介绍

jQuery是目前使用最广泛的javascript函数库。据统计,全世界排名前100万的网站,有46%使用jQuery,远远超过其他库。微软公司甚至把jQuery作为他们的官方库。 

jQuery的版本分为1.x系列和2.x、3.x系列,1.x系列兼容低版本的浏览器,2.x、3.x系列放弃支持低版本浏览器,目前使用最多的是1.x系列的。

jquery是一个函数库,一个js文件,页面用script标签引入这个js文件就可以使用。

<script type="text/javascript" src="js/jquery-1.12.2.js"></script>

jquery的口号和愿望 Write Less, Do More(写得少,做得多) 

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').prevAll('p'); //选择div元素前面所有的p元素

$('div').next('p'); //选择div元素后面的第一个p元素

$('div').nextAll('p'); //选择div元素后面所有的p元素

$('div').closest('form'); //选择离div最近的那个form父元素

$('div').parent(); //选择div的父元素

$('div').children(); //选择div的所有子元素

$('div').siblings(); //选择div的同级元素

$('div').find('.myClass'); //选择div内的class等于myClass的元素

jQuery加载

alert($);//弹出function (a,b){return new n.fn.init(a,b)}表示JQuery已经引进来了,这是它的一个构造函数

JS写法

window.onload = function(){

var oDiv = document.getElementById('div');

alert(oDiv.innerHTML);//这是一个div元素

}

jQuery的完整写法

比上面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');

})

jQuery样式操作

$(function(){

/*jQuery用同一个函数即可以取值、也可以赋值*/

//读取样式

alert($('#div1').css('fontSize'));//16px

//设置(写入)样式

$('#div1').css({background:'gold'});

//增加行间样式

$('#div1').addClass('big');

//减少行间样式,多个样式用空格分开

$('#div1').removeClass('div2 big');

})

click事件

$(function(){

// 给按钮绑定click事件

$('#btn').click(function(){

//重复切换sty样式

$('.box').toggleClass('sty');

})

})

jQuery索引值

$(function(){

$('.list li').click(function(){

// alert(this.innerHTML);//弹出标签中的内容

alert($(this).index());//弹出下标

})

jQuery做选项卡

$(function(){

$('#box1 #btns input').click(function() {

//失去焦点,避免出现默认的蓝框

$(this).blur();

//this是原生的对象

// alert(this);//弹出[object HTMLInputElement],说明this就是当前点击的input元素

//jQuery的this对象使用时要用$()包起来,这样就可以调用jQuery的方法了

//给当前元素添加选中样式,为兄弟元素移除选中样式

$(this).addClass('cur').siblings().removeClass('cur');

//$(this).index()获取当前按钮所在层级范围的索引值

//显示对应索引的内容区,隐藏其它兄弟内容区

$('#box1 #contents div').eq($(this).index()).addClass('active').siblings().removeClass('active');

});

$('#box2 #btns input').click(function() {

$(this).blur();

$(this).addClass('cur').siblings().removeClass('cur');

$('#box2 #contents div').eq($(this).index()).addClass('active').siblings().removeClass('active');

});

})

上一篇 下一篇

猜你喜欢

热点阅读