[英]How do I make my divisions to change color when they are clicked?
我正在做 Odin 項目中的 etch-a-sketch 項目,但遇到了麻煩。 我已經創建了網格並將其顯示在屏幕上,但我似乎無法找到一種方法來讓每個框在被點擊時改變顏色。 我已經嘗試了所有方法,但所有方法都導致每個 div 的邊框消失。
蝕刻草圖.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="reset.css" />
</head>
<body>
<div id="input">
<form id="forrm" method="GET">
<label for="size">Custom Size</label>
<input id="size" type="text" name="size">
</form>
</div>
<div id="container"></div>
<script src="script.js"></script>
</body>
</html>
樣式文件
#container {
border: gray solid 1px;
height: 500px;
width: 500px;
color: green;
margin-left: auto;
margin-right: auto;
position: relative;
margin-top: 30px;
display: grid;
grid-template-columns: repeat(16,1fr);
grid-template-rows: repeat(16,1fr);
}
#input {
display: block;
text-align: center;
}
#forrm {
margin-left: auto;
margin-right: auto;
}
腳本.js
const cont = document.getElementById('container');
for (let a=0; a < 256; a++){
let him = document.createElement('div');
him.classList.add('divi');
him.setAttribute('style','border : solid 1px gray;')
cont.appendChild(him);
}
我必須通過使用 js 來做到這一點。 我嘗試選擇容器中的所有分區,然后遍歷每個分區並添加一個這樣的事件,但它仍然不起作用並導致每個 div 的邊框消失:
腳本.js
let gettin = document.querySelectorAll('divi');
gettin.ForEach((divi) , (e ) {
e.target.setAttribute('click' , 'background-color : blue');
});
查看forEach的文檔。
forEach()為數組中的每個元素按升序調用一次提供的回調函數
使用三個參數調用回調:
- 元素的值
- 元素的索引
- 正在遍歷的 Array 對象
對於我們的示例,我們只對第一個參數感興趣:元素的值。
在回調內部,我們將當前元素保存在標簽divi
。 對於每個divi
,我們可以的addEventListener的click
事件。
const cont = document.getElementById('container'); for (let a = 0; a < 256; a++) { let him = document.createElement('div'); him.classList.add('divi'); him.setAttribute('style', 'border : solid 1px gray;') cont.appendChild(him); } let gettin = document.querySelectorAll('.divi'); gettin.forEach(divi => { divi.addEventListener('click', e => { e.currentTarget.setAttribute('style', 'background-color : blue;') }); });
#container { border: gray solid 1px; height: 500px; width: 500px; color: green; margin-left: auto; margin-right: auto; position: relative; margin-top: 30px; display: grid; grid-template-columns: repeat(16, 1fr); grid-template-rows: repeat(16, 1fr); } #input { display: block; text-align: center; } #forrm { margin-left: auto; margin-right: auto; }
<div id="input"> <form id="forrm" method="GET"> <label for="size">Custom Size</label> <input id="size" type="text" name="size"> </form> </div> <div id="container"> </div>
請參考以下代碼。
const cont = document.getElementById('container'); for (let a = 0; a < 256; a++) { let him = document.createElement('div'); him.classList.add('divi'); him.setAttribute('style', 'border : solid 1px gray;') cont.appendChild(him); } function setBackGroundColor() { this.style.backgroundColor = "blue"; } let gettin = document.getElementsByClassName("divi"); for (var i = 0; i < gettin.length; i++) { gettin[i].addEventListener('click', setBackGroundColor); }
#container { border: gray solid 1px; height: 500px; width: 500px; color: green; margin-left: auto; margin-right: auto; position: relative; margin-top: 30px; display: grid; grid-template-columns: repeat(16, 1fr); grid-template-rows: repeat(16, 1fr); } #input { display: block; text-align: center; } #forrm { margin-left: auto; margin-right: auto; }
<div id="input"> <form id="forrm" method="GET"> <label for="size">Custom Size</label> <input id="size" type="text" name="size"> </form> </div> <div id="container"> </div>
您需要為每個 div 分配 onClick 函數
const cont = document.getElementById('container'); for (let a = 0; a < 256; a++) { let him = document.createElement('div'); him.classList.add('divi'); him.setAttribute('style', 'border : solid 1px gray;') cont.appendChild(him); } let gettin = document.getElementsByClassName('divi'); for (var i = 0; i < gettin.length; i++){ gettin[i].onclick = changeColor; } function changeColor(e){ e.target.setAttribute('style', 'background-color : blue'); }
#container { border: gray solid 1px; height: 500px; width: 500px; color: green; margin-left: auto; margin-right: auto; position: relative; margin-top: 30px; display: grid; grid-template-columns: repeat(16, 1fr); grid-template-rows: repeat(16, 1fr); } #input { display: block; text-align: center; } #forrm { margin-left: auto; margin-right: auto; }
<div id="input"> <form id="forrm" method="GET"> <label for="size">Custom Size</label> <input id="size" type="text" name="size"> </form> </div> <div id="container"> </div>
我相信您想再次單擊時刪除顏色。
為blue
類名添加 css:
.blue{background: blue;}
單擊 div 時使用classList.toggle('blue')
。
最好定義新函數,而不是在每個 div 上附加內聯 JS:
const cont = document.getElementById('container'); for (let a=0; a < 256; a++){ let him = document.createElement('div'); him.classList.add('divi'); him.setAttribute('style','border : solid 1px gray;') cont.appendChild(him); } let gettin = document.querySelectorAll('.divi'); gettin.forEach((e)=>{ e.addEventListener("click",toblue); }); function toblue(){ this.classList.toggle('blue'); }
#container { border: gray solid 1px; height: 500px; width: 500px; color: green; margin-left: auto; margin-right: auto; position: relative; margin-top: 30px; display: grid; grid-template-columns: repeat(16,1fr); grid-template-rows: repeat(16,1fr); } #input { display: block; text-align: center; } #forrm { margin-left: auto; margin-right: auto; } .blue{ background: blue; }
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <link rel="stylesheet" href="style.css" /> <link rel="stylesheet" href="reset.css" /> </head> <body> <div id="input"> <form id="forrm" method="GET"> <label for="size">Custom Size</label> <input id="size" type="text" name="size"> </form> </div> <div id="container"></div> <script src="script.js"></script> </body> </html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.