誰能告訴我為什么這不起作用?目的是在單擊隨機 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;
})
uj5u.com熱心網友回復:
你只呼叫 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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/479003.html
標籤:javascript
