js this指向问题、call、apply、bind、跳转到上一个页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="Keywords" content="关键字,关键词">
    <meta name="Description" content="描述和简介">
    <title>Title</title>
    <style type="text/css">
        *{margin:0;padding:0;}
        body,ul,li,ol,dl,dd,p,h1,h2,h3,h4,h5,h6{ margin:0;}
        a{text-decoration:none;}
        img{border:none;}
        ol,ul{list-style:none;}
        .box{
            margin : 10px;
            width: 100px;
            height: 100px;
            background: pink;
        }
    </style>
</head>
<body>
    <!--
        this指向
            函数执行完之后,this指向默认指向window
        .call()
        .apply()
        .bind()
    -->
    <div class="box">1</div>
    <div class="box">2</div>
    <div class="box">3</div>

    <script>
/*
        Fn();
        function Fn(){
            console.log(this);
        };*/
        var aBox = document.querySelectorAll(".box");
        for(var i = 0; i < aBox.length; i++){
            aBox[i].index = i;
            aBox[i].onclick = function (){
                console.log("紫霞!");
                //chang(this);//传参时,This指向当前对象,否则默认指向window
                chang1.call(this);//每一个函数都有call属性,call则指定函数中的this指向当前的this,点击谁,This就指向谁
            };
        };
/*
        function chang(This){
            console.log(This);
            This.innerHTML = This.index;
            This.className = "box click";
        };*/

        //chang1();
/*        function chang1(){
            console.log(this);
        };*/
/*

        Fn.call(aBox,40,10);//第一个参数代表this指向,只改变当前这一次,后面的参数对应传参
        function Fn(x,y){
            console.log(x+y);
            console.log(this);
        };
        Fn(10,30);
*/
/*
        Fn.apply(aBox , [10,20]);//改变当前这一次,后面的参数采用的是数组
        function Fn(x,y){
            console.log(this);
            console.log(x+y);
        };
*/

    var a = 1;
    document.onclick = function (){
        alert(this.a);//this本来指向的是document,用了bind之后,this指向了window,才能找到a,因为document下没有a,a是在window下的。
        console.log(this);//
    }.bind(window);//改变this指向由document到Window

    </script>
</body>
</html>

js控制跳转到上一个页面

var prevLink = document.referrer;  
if($.trim(prevLink)==''){  
    location.href = 'www.example.com/index.html';  
}else{  
    if(prevLink.indexOf('www.example.com')==-1){    //来自其它站点  
        location.href = 'www.example.com/index.html';  
    }  
    if(prevLink.indexOf('register.html')!=-1){      //来自注册页面  
        location.href = 'www.example.com/index.html';  
    }  
    location.href = prevLink;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值