21个值得收藏的Javascript技巧

前端之家收集整理的这篇文章主要介绍了21个值得收藏的Javascript技巧前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

 
  1. var fruits = ['apple', 'peaches', 'oranges', 'mangoes']; 
  2. var str = fruits.valueOf(); 
 
  1. var fruits = ['apple', 'mangoes']; 
  2. var str = fruits.join("|"); 
 
  1. var str = "apple, peaches, oranges, mangoes";  
  2. var fruitsArray = str.split(","); 
 
  1. function removeByIndex(arr, index) { 
  2.     arr.splice(index, 1); 
  3. test = new Array(); 
  4. test[0] = 'Apple'; 
  5. test[1] = 'Ball'; 
  6. test[2] = 'Cat'; 
  7. test[3] = 'Dog'; 
  8. alert("Array before removing elements: "+test); 
  9. removeByIndex(test, 2); 
  10. alert("Array after removing elements: "+test); 
 
  1. function removeByValue(arr, val) { 
  2.     for(var i=0; i<arr.length; i++) { 
  3.         if(arr[i] == val) { 
  4.             arr.splice(i, 1); 
  5.             break; 
  6.         } 
  7.     } 
  8.   
  9. var somearray = ["mon", "tue", "wed", "thur"] 
  10.   
  11. removeByValue(somearray, "tue"); 
  12.   
  13. //somearray 将会有的元素是 "mon", "thur" 
 
  1. Array.prototype.removeByValue = function(val) { 
  2.     for(var i=0; i<this.length; i++) { 
  3.         if(this[i] == val) { 
  4.             this.splice(i, 1); 
  5.             break; 
  6.         } 
  7.     } 
  8. //.. 
  9. var somearray = ["mon", "thur"] 
  10. somearray.removeByValue("tue"); 
 
  1. var strFun = "someFunction"; //someFunction 为已经定义的方法名 
  2. var strParam = "this is the parameter"; //要传入方法的参数   
  3. var fn = window[strFun]; 
  4.    
  5. //调用方法传入参数 
  6. fn(strParam); 
 
  1. var random = Math.floor(Math.random() * N + 1); 
  2.   
  3. //产生1到10之间的随机数 
  4. var random = Math.floor(Math.random() * 10 + 1); 
  5.   
  6. //产生1到100之间的随机数 
  7. var random = Math.floor(Math.random() * 100 + 1); 
 
  1. <script language="javascript"> 
  2. function fnUnloadHandler() { 
  3.       
  4.        alert("Unload event.. Do something to invalidate users session.."); 
  5. </script> 
  6. <body onbeforeunload="fnUnloadHandler()"> 
  7. ………     
  8. </body> 
 
  1. window.onbeforeunload = function() {  
  2.     return "You work will be lost.";  
  3. }; 
 
  1. function formIsDirty(form) { 
  2.   for (var i = 0; i < form.elements.length; i++) { 
  3.     var element = form.elements[i]; 
  4.     var type = element.type; 
  5.     if (type == "checkBox" || type == "radio") { 
  6.       if (element.checked != element.defaultChecked) { 
  7.         return true; 
  8.       } 
  9.     } 
  10.     else if (type == "hidden" || type == "password" || 
  11.              type == "text" || type == "textarea") { 
  12.       if (element.value != element.defaultValue) { 
  13.         return true; 
  14.       } 
  15.     } 
  16.     else if (type == "select-one" || type == "select-multiple") { 
  17.       for (var j = 0; j < element.options.length; j++) { 
  18.         if (element.options[j].selected != 
  19.             element.options[j].defaultSelected) { 
  20.           return true; 
  21.         } 
  22.       } 
  23.     } 
  24.   } 
  25.   return false; 
  26. window.onbeforeunload = function(e) { 
  27.   e = e || window.event;   
  28.   if (formIsDirty(document.forms["someForm"])) { 
  29.     // IE 和 Firefox 
  30.     if (e) { 
  31.       e.returnValue = "You have unsaved changes."; 
  32.     } 
  33.     // Safari浏览器 
  34.     return "You have unsaved changes."; 
  35.   } 
  36. };
 
  1. <SCRIPT type="text/javascript"> 
  2.     window.history.forward(); 
  3.     function noBack() { window.history.forward(); } 
  4. </SCRIPT> 
  5. </HEAD> 
  6. <BODY onload="noBack();" 
  7.     onpageshow="if (event.persisted) noBack();" onunload=""> 
 
  1. function selectBoxRemove(sourceID) { 
  2.     //获得listBox的id 
  3.     var src = document.getElementById(sourceID); 
  4.     //循环listBox 
  5.     for(var count= src.options.length-1; count >= 0; count--) { 
  6.          //如果找到要删除的选项,则删除 
  7.         if(src.options[count].selected == true) { 
  8.                 try { 
  9.                          src.remove(count, null); 
  10.                            
  11.                  } catch(error) { 
  12.                            
  13.                          src.remove(count); 
  14.                 } 
  15.         } 
  16.     } 
 
  1. function listBoxSelectDeselect(listID, isSelect) { 
  2.     var listBox = document.getElementById(listID); 
  3.     for(var count=0; count < listBox.options.length; count++) { 
  4.             listBox.options[count].selected = isSelect; 
  5.     } 


 
  1. unction listBox_move(listID, direction) { 
  2.    
  3.     var listBox = document.getElementById(listID); 
  4.     var selIndex = listBox.selectedIndex; 
  5.    
  6.     if(-1 == selIndex) { 
  7.         alert("Please select an option to move."); 
  8.         return; 
  9.     } 
  10.    
  11.     var increment = -1; 
  12.     if(direction == 'up') 
  13.         increment = -1; 
  14.     else 
  15.         increment = 1; 
  16.    
  17.     if((selIndex + increment) < 0 || 
  18.         (selIndex + increment) > (listBox.options.length-1)) { 
  19.         return; 
  20.     } 
  21.    
  22.     var selValue = listBox.options[selIndex].value; 
  23.     var selText = listBox.options[selIndex].text; 
  24.     listBox.options[selIndex].value = listBox.options[selIndex + increment].value 
  25.     listBox.options[selIndex].text = listBox.options[selIndex + increment].text 
  26.    
  27.     listBox.options[selIndex + increment].value = selValue; 
  28.     listBox.options[selIndex + increment].text = selText; 
  29.    
  30.     listBox.selectedIndex = selIndex + increment; 
  31. //.. 
  32. //.. 
  33.   
  34. listBox_move('countryList', 'up'); //move up the selected option 
  35. listBox_move('countryList', 'down'); //move down the selected option 
 
  1. function listBox_moveacross(sourceID, destID) { 
  2.     var src = document.getElementById(sourceID); 
  3.     var dest = document.getElementById(destID); 
  4.    
  5.     for(var count=0; count < src.options.length; count++) { 
  6.    
  7.         if(src.options[count].selected == true) { 
  8.                 var option = src.options[count]; 
  9.    
  10.                 var newOption = document.createElement("option"); 
  11.                 newOption.value = option.value; 
  12.                 newOption.text = option.text; 
  13.                 newOption.selected = true; 
  14.                 try { 
  15.                          dest.add(newOption, null); //Standard 
  16.                          src.remove(count, null); 
  17.                  }catch(error) { 
  18.                          dest.add(newOption); // IE only 
  19.                          src.remove(count); 
  20.                  } 
  21.                 count--; 
  22.         } 
  23.     } 
  24. //.. 
  25. //.. 
  26.   
  27. listBox_moveacross('countryList', 'selectedCountryList'); 
 
  1. var numbers = []; 
  2. for(var i=1; numbers.push(i++)<100;); 
  3. //numbers = [0,1,2,3 ... 100] 
  4. 使用的是数组的push方法 
 
  1. var num = 2.443242342; 
  2. alert(num.toFixed(2)); // 2.44 
  3. 而使用toPrecision(x)则提供指定位数的精度,这里的x是全部的位数,如: 
  4. num = 500.2349; 
  5. result = num.toPrecision(4);//输出500.2 
 
  1. if (!Array.prototype.indexOf) {  
  2.     Array.prototype.indexOf = function(obj, start) { 
  3.          for (var i = (start || 0), j = this.length; i < j; i++) { 
  4.              if (this[i] === obj) { return i; } 
  5.          } 
  6.          return -1; 
  7.     } 
  8.   
  9. if (!String.prototype.contains) { 
  10.     String.prototype.contains = function (arg) { 
  11.         return !!~this.indexOf(arg); 
  12.     }; 
 
  1. var hay = "a quick brown fox jumps over lazy dog"; 
  2. var needle = "jumps"; 
  3. alert(hay.contains(needle)); 
 
  1. function removeDuplicates(arr) { 
  2.     var temp = {}; 
  3.     for (var i = 0; i < arr.length; i++) 
  4.         temp[arr[i]] = true; 
  5.   
  6.     var r = []; 
  7.     for (var k in temp) 
  8.         r.push(k); 
  9.     return r; 
  10.   
  11. //用法 
  12. var fruits = ['apple', 'orange', 'peach', 'apple', 'strawberry', 'orange']; 
  13. var uniquefruits = removeDuplicates(fruits); 
  14. //输出的 uniquefruits ['apple', 'strawberry']; 
 
  1. if (!String.prototype.trim) { 
  2.    String.prototype.trim=function() { 
  3.     return this.replace(/^\s+|\s+$/g, ''); 
  4.    }; 
  5.   
  6. //用法 
  7. var str = "  some string    "; 
  8. str.trim(); 
  9. //输出 str = "some string" 
 
  1. window.location.href = "http://viralpatel.net"; 
 
  1. var myOtherUrl =  
  2.        "http://example.com/index.html?url=" + encodeURIComponent(myUrl); 

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

猜你在找的jQuery相关文章