javascript常用積累

來源:互聯網
上載者:User

標籤:避免   dev   java   XML   str   渲染   列印   delegate   value   

一、JS動畫與動作不一致解決:
123 if(!$( "#handle").is(":animated")){//判斷元素是否處於動畫狀態}
二、停止事件冒泡
1234567891011 event.stopPropagation();- 禁止JS報錯window.onerror = function(){return true ; } try {/*try to do*/} catch(e){/*do this if try error */}
三、查看JS對象屬性
1234567 var res = ‘‘ ; var obj = eval( obj );for( var p in eval( obj ) ){var prop = p + ‘:‘ + obj[p] + ‘\n‘ ; res += prop ;}alert( res );
四、頁面重新整理時禁用提交按鈕
123 window.onbeforeunload = function(){$(‘:submit‘).attr(‘disabled‘,true);}

注意:Opera 瀏覽器不支援,其他瀏覽器避免在同一頁面中使用 "javascrpt:" 等偽協議

五、擷取事件
123456789 var getEvent = function(){var ieEvent = window.event ; var ffEvent = arguments.callee.caller.arguments[0] ; //arguments.callee 當前執行函數//arguments.callee.caller 當前執行函數的調用者//arguments.callee.caller.arguments[0] 當前函數調用者的第一個參數var e = ieEvent || ffEvent ; return e ;}
六、擷取鍵盤碼
1234567 var getKCode = function(){var ieEvent = window.event ; var ffEvent = arguments.callee.caller.arguments[0] ; var e = ieEvent || ffEvent ; var kCode = e.keyCode || e.which ;return kCode ;}
七、 滑鼠滑入/滑出樣式切換
123 $("div").on("mouseover mouseout", function(){$(this).toggleClass("over");});
八、點擊滑鼠,顯示/隱藏切換
1234567 $("#panel h5.head").toggle(function(){$(this).toggleClass("highlight");$(this).next().toggle();},function(){$(this).toggleClass("highlight");$(this).next().toggle();});
九、JS 調試
12345 console.log() ; //列印變數console.dir() ; //列印對象console.dirxml() ; //列印節點console.trace() ; //列印函數調用軌跡window.document.title = str;
十、為子項目集合綁定事件
123 $("div").delegate("button","click",function(){$("p").slideToggle();});
十一、自訂IE瀏覽器渲染方式(解決IE10下JS或外掛程式失效):

如果安裝了Chrome核心,則使用Chrome核心來渲染頁面[chrome=1],如果未安裝,則使用最高版本的IE核心進行渲染[IE=edge]:

1 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
十二、註冊事件
12345 // 標準瀏覽器form1.addEventListener(‘submit‘, function(e){e.preventDefault(); //阻止瀏覽器預設動作e.stopPropagation(); //阻止事件流產生});
12345 // IE8及更早版本IE瀏覽器form1.attachEvent(‘submit‘, function(){event.cancelBubble = true; //阻止瀏覽器預設動作--IE8及更早版本IE瀏覽器event.returnValue = false; //阻止事件流產生--IE8及更早版本IE瀏覽器}

javascript常用積累

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.