css部分
<style>
*{
/* 取消文字双击颜色 */
user-select: none;
}
.box {
width: 180px;
height: 60px;
background-color: #ccc;
text-align: center;
}
span:nth-child(1) {
color: yellow;
font-size: 20px;
}
span:nth-child(2) {
color: rgb(141, 141, 19);
font-size: 30px;
}
span:nth-child(3) {
color: rgb(104, 45, 201);
font-size: 16px;
}
span:nth-child(4) {
color: rgb(240, 50, 50);
font-size: 40px;
}
</style>
<div class="box">
<span>a</span>
<span>b</span>
<span>c</span>
<span>d</span>
</div>
js部分
<script>
// 取到随机数中可能出现的大小写英文和数字
let arr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let box = document.querySelector('.box')
box.onclick = () => {
let arr1 = '';
for (let i = 0; i < 4; i++) {
// Math.floor()为向下取整
// 随机文字大小
let fz = Math.floor(Math.random() * 20 + 20)
// 颜色rgb中的随机r
let r = Math.floor(Math.random() * 256)
// 颜色rgb中的随机g
let g = Math.floor(Math.random() * 256)
// 颜色rgb中的随机b
let b = Math.floor(Math.random() * 256)
// 随机arr字符串
let num=Math.floor(Math.random()*arr.length)
arr1=arr1+'<span style="font-size:'+fz+'px;color:rgb('+r+','+g+','+b+')">'+arr[num]+'</span>'
}
box.innerHTML=arr1;
}
</script>
思路大致就是这样 如有不足之处 还请见谅!!!
另外 下面为封装函数
<!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>Document</title>
<style>
*{
/* 取消文字双击颜色 */
user-select: none;
}
.box {
width: 180px;
height: 60px;
background-color: #ccc;
text-align: center;
}
span:nth-child(1) {
color: yellow;
font-size: 20px;
}
span:nth-child(2) {
color: rgb(141, 141, 19);
font-size: 30px;
}
span:nth-child(3) {
color: rgb(104, 45, 201);
font-size: 16px;
}
span:nth-child(4) {
color: rgb(240, 50, 50);
font-size: 40px;
}
</style>
</head>
<body>
<div class="box">
<span>a</span>
<span>b</span>
<span>c</span>
<span>d</span>
</div>
<script>
// 取到随机数中可能出现的大小写英文和数字
let arr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let box = document.querySelector('.box')
box.onclick = () => {
rom(box,arr,4);
}
function rom(className,array,n){
let arr1 = '';
for (let i = 0; i < n; i++) {
let fz = Math.floor(Math.random() * 20 + 20)
let r = Math.floor(Math.random() * 256)
let g = Math.floor(Math.random() * 256)
let b = Math.floor(Math.random() * 256)
let num=Math.floor(Math.random()*array.length)
arr1=arr1+'<span style="font-size:'+fz+'px;color:rgb('+r+','+g+','+b+')">'+array[num]+'</span>'
}
className.innerHTML=arr1;
}
</script>
</body>
</html>