zoukankan      html  css  js  c++  java
  • js面对对象和jQuery的使用

    闭包

    函数嵌套函数,内部函数可以引用外部函数的参数和变量,参数和变量不会被垃圾回收机制收回.

    用处

    • 1、将一个变量长期驻扎在内存当中,可用于循环中存索引值
    <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>
    
    • 2、私有变量计数器,外部无法访问,避免全局变量的污染
    <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>
    

    js的内置对象:

    1.document

    document.referrer //获取上一个跳转页面的地址(需要服务器环境)

    2.location

    window.location.href //获取或者重定url地址
    window.location.search //获取地址参数部分
    window.location.hash //获取页面锚点或者叫哈希值

    3.Math

    Math.random 获取0-1的随机数
    Math.floor 向下取整
    Math.ceil 向上取整

    面向对象

    面向过程与面向对象编程

    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>
    

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

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

    • jquery是一个函数库,一个js文件,页面用script标签引入这个js文件就可以使用。
    <script type="text/javascript" src="js/jquery-1.12.2.js"></script>
    

    jquery加载

    将获取元素的语句写到页面头部,会因为元素还没有加载而出错,jquery提供了ready方法解决这个问题,它的速度比原生的 window.onload 更快。

    <script type="text/javascript">
    
    $(function(){
         代码
    });
    </script>
    

    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的元素
    

    jquery样式操作

    jquery用法思想二

    同一个函数完成取值和赋值

    操作行间样式

    // 获取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样式
    

    jquery属性操作

    • 1、html() 取出或设置html内容
    // 取出html内容
    var $htm = $('#div1').html();
    
    // 设置html内容
    $('#div1').html('<span>添加文字</span>');
    
    • 2、text() 取出或设置text内容
    // 取出文本内容
    var $htm = $('#div1').text();
    
    // 设置文本内容
    $('#div1').text('<span>添加文字</span>');
    
    • 3、attr() 取出或设置某个属性的值
    // 取出图片的地址
    var $src = $('#img1').attr('src');
    
    // 设置图片的地址和alt属性
    $('#img1').attr({ src: "test.jpg", alt: "Test Image" });
    

    绑定click事件

    给元素绑定click事件,可以用如下方法:

    $('#btn1').click(function(){
    
    // 内部的this指的是原生对象
    
    // 使用jquery对象用 $(this)
    })
    
    $(function(){
    // 给按钮绑定click事件
    $('#btn').click(function(){
    //重复切换sty样式
    $('.box').toggleClass('sty');
    	})
    })
    
  • 相关阅读:
    一卦,测一年运气
    测一下我心中想的事
    一卦,测一下我心里想的事
    这一卦,学到了不少东西
    癸山丁向下卦(七运)
    起卦测我心里想的事
    现在的卦,越来越看不懂了
    luogu P2759 奇怪的函数 |二分答案
    luogu P2515 [HAOI2010]软件安装 |Tarjan+树上背包
    luogu P2343 宝石管理系统 |分块+堆
  • 原文地址:https://www.cnblogs.com/lygiants/p/11080774.html
Copyright © 2011-2022 走看看