javascript中 有用的小方法

来源:互联网 发布:下载头像的软件 编辑:程序博客网 时间:2024/06/07 07:50
<!DOCTYPE html><html lang="en">    <head>        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">        <meta name="Author" content="">        <meta name="Keywords" content="">        <meta name="Description" content="">        <title>Document</title>        <style type="text/css">            * {margin: 0; padding: 0;}            a {text-decoration: none;}            ul,li {list-style: none;}            body {font-family: "Microsoft yahei";}        </style>    </head><body><script type="text/javascript">    // javascript中 有用的方法    /*        1.两个变量交换值                //不用第三个变量     */    var a = "一";    var b = "二";    a = [b,b=a][0];  //再赋值前,b的值先保留下来了.    console.log(a,b);    /*        2.定时器传参        setTimeout        setInterval                //不用有名函数来传.     */    setTimeout(function(a,b){        console.log(a+b);    },1000,1,5);  //第二参数开始一一对应 a,b    /*        3.字符串的换行                //不用切割     */    document.body.innerHTML = "<div>\                                    <p>\                                        <a href='#'>内容</a>\                                    </p>\                                </div>";        /*            4.for嵌套的跳出         */         // 添上名        name:for (var i = 1; i < 3; i++) {            for(var j = 0;j<3;j++){                console.log(i*j);                if(i*j == 1){                    break name; //这样只是跳出内循环.                }            }        }        /*            5.数组取最大值         */        var arr = [12,432,5,65,6,7,8];        // arr.sort(function(a,b){        //     return b>a;        // })        // console.log(arr[0]);        // 不改变数组顺序        // Math.max(12,432,5,65,6,7,8);        console.log(Math.max.apply(Math,arr));        this.c = 1;        function fn(a,b){            alert(this.c+a+b);        };        fn.apply(window,[2,3]);</script></body></html>
0 0