<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>随机密码</title> </head> <body> </body> </html> <script> // 结果 var res = ''; // 数字+大小写字母+特殊字符 var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+~`|}{[]:;?><,./-='; // 设定的密码长度 var length = 8; for (var i = 0; i < length; i++) { var item = Math.floor(Math.random() * str.length); res += str.charAt(item); } document.getElementsByTagName('body')[0].innerText = res; console.log(res); </script>
ps:如果你想给网页插入一张图片,可以用innerHTML,如:
document.getElementsByTagName('body')[0].innerHTML = "<img src='https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png' >";
标签:12,08,innerHTML,随机,2023,var From: https://www.cnblogs.com/iuniko/p/17624831.html