标签:style Javascript 网页 random 获取 直接 round Math 255
获取元素 getElements;
按标签名称获取元素 getElementsByTagName;
按名称获取元素 getElementByName;
按类名获取元素 getElementsByClassName;
通过ID 获取元素 getElementById
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>在网页中直接使用<script>js code</script></title>
<meta name="Keywords" content="关键字">
<meta name="description" content="简介">
<style>
p {
transition: all .3s;
}
</style>
</head>
<body>
<p>Lorem ipsum dolor.</p>
<p>Assumenda, enim, odit.</p>
<p>Aut, praesentium, quisquam!</p>
<p>Eligendi, excepturi, ullam?</p>
<p>Delectus praesentium, repudiandae?</p>
<p>Amet, laudantium, temporibus?</p>
<script>
/*获取元素 getElements;
*按标签名称获取元素 getElementsByTagName
*按名称获取元素 getElementsByName
*按类名获取元素 getElementsByClassName
*通过 ID 获取元素 getElementById
* */
let ps = document.getElementsByTagName('p')
for (let p of ps) {
p.style.border = "1px solid gray"
p.style.padding = '10px'
// p.style.width = '300px'
//随机宽度
p.style.width = `${Math.round(Math.random() * 300) + 600}px`
// p.style.backgroundColor = 'pink'
//随机颜色
p.style.backgroundColor = `rgba(${Math.round(Math.random() * 255)},${Math.round(Math.random() * 255)},${Math.round(Math.random() * 255)},${Math.random()})`
}
setInterval(() => {
let ps = document.getElementsByTagName('p')
for (let p of ps) {
p.style.border = "1px solid gray"
p.style.padding = '10px'
//随机宽度
p.style.width = `${Math.round(Math.random() * 300) + 600}px`
//随机背景颜色
p.style.backgroundColor = `rgba(${Math.round(Math.random() * 255)},${Math.round(Math.random() * 255)},${Math.round(Math.random() * 255)},${Math.random()})`
//随机字体颜色
p.style.color = `rgb(${Math.round(Math.random() * 255)},${Math.round(Math.random() * 255)},${Math.round(Math.random() * 255)}`
}
}, 2000)
</script>
</body>
</html>
标签:style,Javascript,网页,random,获取,直接,round,Math,255 来源: https://blog.csdn.net/W_SJ_SY/article/details/120794229
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。