js 提示 - [近幾年工作中的經驗總結的技巧]

來源:互聯網
上載者:User

標籤:javascript

1、如果 ajax 返回單一的 json 格式,接收方需要這樣再格式化一下賦值: var str = eval("(" + msg + ")");

開發引用: /// <reference path="http://x.autoimg.cn/as/static/js/jquery-1.7.2.min.js" />2、如果 ajax 同發出兩個以上的請求時,返回的狀態會串,不能用非同步,用同步可以解決問題;3、navigator.plugins["Shockwave Flash"] 檢查 當前機器 flash 版本4、刪除前後空格 String.prototype.trim = function () { return this.replace(/(^[ | ]*)|([ | ]*$)/g, ""); }5、IE6 下 JS 在執行一個方法後,當前方法內的代碼執行完後,此次的單線程就會停止,當前的方法裡還有其它的方法也不會再執行;如果要執行的話,需要加 setTimeout(); 事件再執行;6、把 document.getElementById(id) 轉換成 $("id")function $(id) { return typeof (id) == ‘string‘ ? document.getElementById(id) : id }7、圖片載入失敗,並防止死迴圈 onerror="this.src=aaa.jpg;this.onerror=null;"8、document.getElementsByTagName(‘*‘).length 查看頁面有多少個 Dom 元素;9、parseInt() 只會返回整數部分;一個完整的parseInt應該是這樣的:parseInt(string, radix),其中radix指定數位進位(十進位,二進位,十六進位etc.) parseInt("f",16): 15把加號放在包含合法數位字串前面會將字串轉化為數字;Null 用成數字時會表現為0,做布爾時表現為false.聲明一個變數但沒有賦值,此時這個變數的值為undefined. Undefined用作數字時類型表現為NaN, 用作布爾時表現為false.10、各種正則驗證規則 數字驗證規則:"^\\d+$"  //非負整數(正整數 + 0) "^[0-9]*[1-9][0-9]*$"  //正整數 "^((-\\d+)|(0+))$"  //非正整數(負整數 + 0) "^-[0-9]*[1-9][0-9]*$"  //負整數 "^-?\\d+$"    //整數 "^\\d+("  //非負浮點數(正浮點數 + 0) "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮點數"^((-\\d+("  //非正浮點數(負浮點數 + 0) "^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //負浮點數"^(-?\\d+)("  //浮點數var r = /^\+?[1-9][0-9]*$/;  //正整數
r.test(str);11、按照 json 的屬性值排序var cc=[{ name: "a", age: 30},{ name: "c", age: 24},{ name: "b", age: 28},{ name: "e", age: 18},{ name: "d", age: 38}].sort(function(obj1, obj2) {return obj1.age - obj2.age;});for(var i=0;i<cc.length;i++){alert(cc[i][‘age‘]); //依次顯示 18,24,28,30,38}12、合并數組:var mergeTo = [4,5,6];var mergeFrom = [7,8,9];Array.prototype.push.apply(mergeTo, mergeFrom);mergeTo; // is: [4, 5, 6, 7, 8, 9]13、多點擊事件擷取點擊的是哪個$(‘#IndexLink,#IndexLink1‘).on(‘click‘, function (e) {var id=e.target.id;
id 取到的就是被點擊的ID值
}14、zepto 裡面或者 jquery 裡返回 ajax json 串時,如果你的 json 串不規範(裡面的鍵名,值,必須是雙引號),就會跳出 success 方法,而且不執行裡面的所有語句;15、再JavaScript中下面做法會聲明全域變數在function外使用聲明變數(無論是否使用var) ;在function內不是用var 聲明變數 ;直接賦值於window屬性;16、錨點定位方法window.location.hash = ‘m001‘;<a name="m001">&nbsp;</a>17、window.history.forward(1); 阻止頁面後退;18、 JS call 與aplly 用法 function Person(name, age) {this.name = name;this.age = age;};function Student(name, age, grade) {Person.apply(this, arguments);this.grade = grade;};var student = new Student(‘qian‘, 21, ‘一年級‘);alert(‘name:‘ + student.name + ‘\n‘ + ‘age:‘ + student.age + ‘\n‘ + ‘grade:‘ + student.grade);//也就是通俗一點講就是:用student去執行Person這個類裡面的內容,在Person這個類裡面存在this.name等之類的語句,//這樣就將屬性建立到了student對象裡面 18)頁面到底部自動載入內容:var divH = document.body.scrollHeight,top = document.body.scrollTop,windowH = window.screen.availHeight;if ((top + windowH) >divH) {console.log(‘該他媽的載入內容了。‘);}console.log(‘網頁本文全文高:‘ + document.body.scrollHeight + ‘ 網頁被捲去的高: ‘ + document.body.scrollTop + ‘ 螢幕可用工作區高度:‘ + window.screen.availHeight);

其他精彩文章

jQuery教程(19)-jquery ajax操作之序列化表單jQuery教程(18)-ajax操作之執行POST請求jQuery教程(20)-jquery ajax + php 操作之為Ajax請求提供不同...jQuery教程(21)-jquery ajax 回呼函數jQuery教程(22)-ajax操作之錯誤處理jQuery教程(24)-ajax操作之Ajax和事件

更多關於android開發文章


js 提示 - [近幾年工作中的經驗總結的技巧]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.