版权声明:转载请指明出处 https://blog.csdn.net/weixin_42321963/article/details/81937433
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<p>hello01</p>
<p>hello02</p>
<p>hello03</p>
<p>hello04</p>
<p>hello05</p>
<p>hello06</p>
<p>hello07</p>
<p>hello08</p>
<p>hello09</p>
<p>hello10</p>
<script>
//颜色函数,返回随机色
function c() {
var str = "0123456789abcdef";
var cc = "#";
cc += str.charAt(Math.round(Math.random() * (str.length - 1)));
cc += str.charAt(Math.round(Math.random() * (str.length - 1)));
cc += str.charAt(Math.round(Math.random() * (str.length - 1)));
cc += str.charAt(Math.round(Math.random() * (str.length - 1)));
cc += str.charAt(Math.round(Math.random() * (str.length - 1)));
cc += str.charAt(Math.round(Math.random() * (str.length - 1)));
return cc;
}
//长度函数,返回随机长度
function long() {
var dd = Math.round(Math.random() * 100);
dd = dd + "%";
return dd;
}
//为p调用随机颜色和随机长度
var ad = document.getElementsByTagName("p");
for (var o of ad) {
o.style.backgroundColor = c();
o.style.color = c();
o.style.width = long();
}
</script>
</body>
</html>
结果如下 :