簡體   English   中英

EventListener 使用 JS 創建 RGB 調色板的問題

[英]Problem with EventListener to create a RGB color palette with JS

我正在嘗試使用 2 塊創建調色板。 我的問題是我的代碼給了我相同顏色的那些塊,我怎樣才能將兩者分開以產生兩種不同的顏色? 我希望我很清楚我的英語不是那么好。 提前致謝!

 const button = document.querySelector("button"); const blockone = document.querySelector("#block1"); const blocktwo = document.querySelector("#block2"); button.addEventListener("click", function() { const r = Math.floor(Math.random() * 255); const g = Math.floor(Math.random() * 255); const b = Math.floor(Math.random() * 255); const newColor = `rgb(${r}, ${g}, ${b})` blockone.style.backgroundColor = newColor; blockone.innerText = newColor const r2 = Math.floor(Math.random() * 255); const g2 = Math.floor(Math.random() * 255); const b2 = Math.floor(Math.random() * 255); const newColor2 = `rgb(${r}, ${g}, ${b})` blocktwo.style.backgroundColor = newColor2; blocktwo.innerText = newColor2 })
 <div id="block1"></div> <div id="block2"></div> <button>click</button>

您的代碼中有錯字。 將 'const newColor2 = rgb(${r}, ${g}, ${b}) ' 替換為 'const newColor2 = rgb(${r2}, ${g2}, ${b2}) '

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM