方法一(其实是毫秒时间数字字符串): 

Javascript代码  使用Javascript实现随机字符串
  1. function randomString() {  
  2.     return '' + new Date().getTime();  
  3. }  



  方法二(随机字母数字字符串): 

Javascript代码  使用Javascript实现随机字符串
    1. var alpha_num_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');  
    2.   
    3. function randomString(length) {  
    4.       
    5.     if (! length) {  
    6.         length = Math.floor(Math.random() * alpha_num_chars.length);  
    7.     }  
    8.       
    9.     var str = '';  
    10.     for (var i = 0; i < length; i++) {  
    11.         str += alpha_num_chars[Math.floor(Math.random() * alpha_num_chars.length)];  
    12.     }  
    13.     return str;  
    14. }  
    15.   
    16. // generate a random string of random length  
    17. randomString();  
    18.   
    19. // generate a random string of length 8  
    20. randomString(8);  

相关文章:

  • 2022-12-23
  • 2021-08-31
  • 2021-05-26
  • 2022-12-23
  • 2022-12-23
  • 2021-07-28
  • 2022-02-12
  • 2022-03-10
猜你喜欢
  • 2022-12-23
  • 2021-07-16
  • 2021-07-17
  • 2022-12-23
  • 2022-02-08
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案