jquery技巧(持续更新。。)
生活随笔
收集整理的這篇文章主要介紹了
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
總結
以上是生活随笔為你收集整理的jquery技巧(持续更新。。)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 参观中央财经大学机房安装centos6.
- 下一篇: 万能写入sql语句,并且防注入