谁能告诉我为什么这不起作用?目的是在单击随机 rgb 颜色时更改标题(onclick 功能甚至没有出现在我的 gitpage 上:https ://lucaspaulii.github.io/portfolio/ )
const title = document.getElementById("title");
let randNum = () => {
let num = Math.floor(Math.random() * 255);
return num
}
let randColor = () => {
return 'rgb('+ randNum() + ',' + randNum() + ',' + randNum() + ')'
}
let newColor = randColor();
title.addEventListener('click', function onClick() {
title.style.color = newColor;
})
你只调用 randColor 一次,在处理程序内部调用它:
const title = document.getElementById("title");
let randNum = () => {
let num = Math.floor(Math.random() * 255);
return num
}
let randColor = () => {
return 'rgb('+ randNum() + ',' + randNum() + ',' + randNum() + ')'
}
title.addEventListener('click', function onClick() {
title.style.color = randColor();
})
<div id="title">click here</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句