1.JS操作数组过滤

    1. var arr=[
    2. {value:1,item:'A},
    3. {value:2,item:'B},
    4. {value:3,item:'C},
    5. {value:4,item:'D},
    6. ];
    7. var checkArr=['1','2','4'];
    8. let newArr= arr.filter(item=>{
    9. if(checkArr.map(Number).includes(item.value)!=-1){
    10. return item
    11. }
    12. });
    13. console.log(newArr,'筛选出来的新数组')

    2.JS生成随机颜色

    1. (Math.floor(Math.random()*255+1),Math.floor(Math.random()*255+1),Math.floor(Math.random()*255+1))

    3.输入电话验证 空格 reg

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no, viewport-fit=cover" />
    5. <meta charset="UTF-8">
    6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    7. <title>微信支付</title>
    8. <!-- <link rel="stylesheet" href="./css/reset.css"> -->
    9. </head>
    10. <body>
    11. <div id="main">
    12. <header>
    13. 众成鑫富
    14. </header>
    15. <main>
    16. <div class="iptBox">
    17. <input type="text" placeholder="请输入您的充值账号!" id="ipt">
    18. <div class="info">
    19. <span class="errTip"></span>
    20. </div>
    21. </div>
    22. </main>
    23. <footer>
    24. <button id="payBtn">支付</button>
    25. </footer>
    26. </div>
    27. <script>
    28. const waitDuration=500;
    29. //获取dom节点,为按钮绑定点击事件(加入节流功能函数)
    30. var payBtn = document.querySelector('#payBtn');
    31. var telPhoneVal=document.querySelector('#ipt');
    32. var errTip=document.querySelector('.errTip');
    33. //定义两个缓存值
    34. var firstLen = 0;
    35. var lastLen = 0;
    36. var reg = /^1(3|4|5|7|8)\d{9}$/;
    37. payBtn.addEventListener("click", clickFn(clickEvent, waitDuration));
    38. // 节流功能函数,防止用户多次触发
    39. function clickFn(fn, delay) {
    40. let timer;
    41. return function () { // 这里是标签的点击事件调用该匿名函数,所以该匿名函数指向<button id="clickEventDom">点击</button>
    42. if (timer) {
    43. return;
    44. }
    45. let that = this;
    46. let arg = arguments;
    47. timer = setTimeout(function () {
    48. fn.apply(that, arg);
    49. timer = null;
    50. }, delay)
    51. }
    52. };
    53. // 支付函数,一下为支付业务功能代码
    54. function clickEvent() {
    55. console.log('支付功能');
    56. window.location.href='./success.html';
    57. };
    58. // 手机号验证
    59. telPhoneVal.oninput = function () {
    60. telPhoneVal.value = telPhoneVal.value.substr(0, 13); //只允许输入11位+2个空格
    61. //用户输入满11位开始验证
    62. if (telPhoneVal.value.length == 13) {
    63. //将数据去掉空格后验证
    64. if (!reg.test(telPhoneVal.value.replace(/\s/g, ''))) {
    65. errTip.innerHTML = '*'+' '+'您输入的手机账号有误,请重新输入'
    66. } else {
    67. errTip.innerHTML = ''
    68. }
    69. } else {
    70. errTip.innerHTML = ''
    71. }
    72. };
    73. // 手机号四位分割
    74. telPhoneVal.onfocus = function () {
    75. timer = setInterval(function () {
    76. lastLen = telPhoneVal.value.length;
    77. if (lastLen > firstLen) {
    78. firstLen = telPhoneVal.value.length;
    79. if (lastLen === 4 || lastLen === 9) {
    80. var temp1 = telPhoneVal.value.substr(0, telPhoneVal.value.length - 1);
    81. var temp2 = telPhoneVal.value.substr(telPhoneVal.value.length - 1);
    82. telPhoneVal.value = temp1 + ' ' + temp2;
    83. }
    84. } else if (lastLen <= firstLen) {
    85. if (lastLen === 4 || lastLen === 9) {
    86. telPhoneVal.value = telPhoneVal.value.substr(0, telPhoneVal.value.length - 1);
    87. }
    88. firstLen = telPhoneVal.value.length;
    89. }
    90. }, 10); //如果手机出现卡顿,可适当把定时器时间加大
    91. }
    92. </script>
    93. <style>
    94. * {
    95. padding: 0;
    96. margin: 0;
    97. }
    98. body,
    99. html {
    100. width: 100%;
    101. height: 100%;
    102. }
    103. #main {
    104. width: 100%;
    105. height: 100%;
    106. background-color: #f2f2f2;
    107. display: flex;
    108. justify-content: space-between;
    109. flex-wrap: nowrap;
    110. flex-direction: column;
    111. }
    112. header {
    113. height: 26px;
    114. width: 100%;
    115. background-color: #f2f2f2;
    116. display: flex;
    117. justify-content: center;
    118. align-items: center;
    119. font-size: 16px;
    120. font-family: 'Courier New', Courier, monospace;
    121. color: #333;
    122. font-weight: bolder;
    123. }
    124. main {
    125. flex: 1;
    126. width: 100%;
    127. height: 100%;
    128. overflow-y: scroll;
    129. background: #fff;
    130. }
    131. .iptBox{
    132. display: flex;
    133. height: 22%;
    134. width: 100%;
    135. flex-direction: column;
    136. justify-content: center;
    137. align-items: center;
    138. }
    139. .iptBox input{
    140. display: inline;
    141. height: 50px;
    142. width: 80%;
    143. outline: none;
    144. border: none;
    145. border-bottom: 1px solid #a8a8a8;
    146. font-size: 26px;
    147. font-weight: bolder;
    148. font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    149. }
    150. .info{
    151. width: 80%;
    152. margin: 0 auto;
    153. height: 40px;
    154. margin-top: 5px;
    155. color: #f55c64;
    156. font-size: 13px;
    157. }
    158. footer {
    159. height: 50px;
    160. width: 100%;
    161. background-color: rgba(255, 255, 255, 0.4);
    162. display: flex;
    163. justify-content: center;
    164. align-items: center;
    165. }
    166. button {
    167. width: 140px;
    168. line-height: 38px;
    169. text-align: center;
    170. font-weight: bold;
    171. color: #fff;
    172. text-shadow: 1px 1px 1px #333;
    173. border-radius: 5px;
    174. position: relative;
    175. overflow: hidden;
    176. border: 1px solid #64c878;
    177. box-shadow: 0 1px 2px #b9ecc4 inset, 0 -1px 0 #6c9f76 inset, 0 -2px 3px #b9ecc4 inset;
    178. background: -webkit-linear-gradient(top, #90dfa2, #84d494);
    179. background: -moz-linear-gradient(top, #90dfa2, #84d494);
    180. background: linear-gradient(top, #90dfa2, #84d494);
    181. }
    182. button:hover {
    183. background: -webkit-linear-gradient(top, #aaebb9, #82d392);
    184. background: -moz-linear-gradient(top, #aaebb9, #82d392);
    185. background: linear-gradient(top, #aaebb9, #82d392);
    186. }
    187. </style>
    188. </body>
    189. </html>