jquery技巧(持续更新。。)

(1)集合处理功能

        //为索引为0,1,2的元素分别设置不同的字体颜色
        $('p').each(function(i){
              this.style.color=['#f00','#0f0','#00f'][i];
        });
        //实现表格的隔行换色
        $('tr').each(function(){
              this.style.backgroundColor=['#ccc','#fff'][i%2];
        });
(2)toggle(evenfn,oddfn) //每次点击轮流调用这两个函数
(3)$.merge(arr1,arr2) //合并两个数组,并删除其中的重复项
(4)$.trim(str) //删除字符串两端的空白字符
(5)blur,focus事件:失去焦点、获得焦点
(6)指定a标签,即rel='external',在新窗口打开界面
        $("a[rel='external']").click(function(){
            this.target='_blank';
        });
(7)所有a标签在新窗口打开界面
        $('a').attr('target','_blank');
(8)点击后,在原窗口打开界面
        $('dom').on('click',function(){
            window.open(url,'_self');//window.location.href=url
        });
       点击后,在新窗口打开界面
        $('dom').on('click',function(){
            window.open(url,'_blank');
        });
(9)禁止右键弹出
        $(document).on('contextmenu',function(){ return false; });
        突破方法:
        javascript:alert($(document).unbind('contextmenu',''));
(10)function test(){alert(arguments.length);}
        test(1,2)  //alert(2)
        test('a','b',1,2,3)  //alert(5)
        利用arguments,对相同函数传递不同个数参数时,进行不同处理
        递归函数,为了降低耦合,使用arguments.callee()调用当前函数
        严格模式下,无法访问arguments.callee()属性,可使用如下方法:
        var func = (function f(num){
          if(num <= 1){
            return 1;
          }else{
            return num * f(num-1);
          }
        });
(11)无块级作用域,花括号内部(if,for语句之类,不包括函数)定义的变量,当前执行环境仍可访问(不是全局环境)。
(12)js中,用push()、pop()模拟栈方法,shift()、push()或unshift()、pop()方法模拟队列
(13)//错误做法
        if(condition){
          function test(){alert('test1');};
        }else{
          function test(){alert('test2');};
        }
 
          //正确做法
        var test;
        if(condition){
          test = function(){alert('test1');};
        }else{
          test = function(){alert('test2');};
        }
原文地址:https://www.cnblogs.com/hitbs228/p/3688872.html