js原生简单生成卡密例子
生活随笔
收集整理的這篇文章主要介紹了
js原生简单生成卡密例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個非常非常簡單的案例
?例子可指定生成卡密數量和長度
先看生成卡密的函數
//生成卡密函數function randomWord(length) {var str = '',arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x','y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V','W', 'X', 'Y', 'Z'];//每位隨機字符 for (let i = 0; i < length; i++) {pos = Math.round(Math.random() * (arr.length - 1));str += arr[pos];}return str;}思路就是隨機拿到arr數組中提前設定好的值組合輸出
看完整實例
<body><div><input type="text" class="nmb" placeholder="輸入卡密數量"><br><br><input type="text" class="length" placeholder="輸入卡密長度,例如16"><br><br><button onclick="key()">生成</button></div><div class="key"><ul></ul></div><script>//生成卡密函數function randomWord(length) {var str = '',arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x','y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];//每位隨機字符 for (let i = 0; i < length; i++) {pos = Math.round(Math.random() * (arr.length - 1));str += arr[pos];}return str;}//點擊事件function key() {var nmb = document.querySelector('.nmb').value; //獲取卡密數量var long = document.querySelector('.length').value; //獲取卡密長度document.querySelector('.key ul').innerHTML = ''; //清空舊的卡密列表//循環輸出卡密for (let i = 0; i < nmb; i++) {var key = randomWord(long);console.log(key.length);var li = document.createElement('li');li.innerHTML = key;document.querySelector('.key ul').appendChild(li);}}</script> </body>生成一下
?
總結
以上是生活随笔為你收集整理的js原生简单生成卡密例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端学习(2003)vue之电商管理系统
- 下一篇: 前端学习(1653):前端系列实战课程之