设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

21个值得收藏的Javascript技巧

2013-8-2 10:57| 发布者: 红黑魂| 查看: 2318| 评论: 0|来自: 51cto

摘要: 在本文中列出了21个值得收藏的Javascript技巧,在实际工作中,如果能适当运用,则大大提高工作效率。 1 Javascript数组转换为CSV格式 首先考虑如下的应用场景,有一个Javscript的字符型(或者数值型)数组,现在需要转换为 ...

13 Listbox中项目的上下移动

下面的代码,给出了在一个listbox中如何上下移动项目

  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 

14 在两个不同的Listbox中移动项目

如果在两个不同的Listbox中,经常需要在左边的一个Listbox中移动项目到另外一个Listbox中去,下面是相关代码:

  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'); 

15 快速初始化Javscript数组

下面的方法,给出了一种快速初始化Javscript数组的方法,代码如下:

  1. var numbers = []; 
  2. for(var i=1; numbers.push(i++)<100;); 
  3. //numbers = [0,1,2,3 ... 100] 
  4. 使用的是数组的push方法 

16 截取指定位数的小数

如果要截取小数后的指定位数,可以使用toFixed方法,比如:

  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 

17 检查字符串中是否包含其他字符串

下面的代码中,可以实现检查某个字符串中是否包含其他字符串。代码如下:

  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.     }; 

在上面的代码中重写了indexOf方法并定义了contains方法,使用的方法如下:

  1. var hay = "a quick brown fox jumps over lazy dog"
  2. var needle = "jumps"
  3. alert(hay.contains(needle)); 

18  去掉Javscript数组中的重复元素

下面的代码可以去掉Javascript数组中的重复元素,如下:

  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', 'orange', 'peach', 'strawberry']; 

19  去掉String中的多余空格

下面的代码会为String增加一个trim()方法,代码如下:

  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" 

20 Javascript中的重定向

在Javascript中,可以实现重定向,方法如下:

  1. window.location.href = "http://viralpatel.net"

21 对URL进行编码

有的时候,需要对URL中的传递的进行编码,方法如下:

  1. var myOtherUrl =  
  2.        "http://example.com/index.html?url=" + encodeURIComponent(myUrl); 

原文链接:http://viralpatel.net/blogs/javascript-tips-tricks/


酷毙
2

雷人

鲜花

鸡蛋

漂亮

刚表态过的朋友 (2 人)

  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部