我想用onclick
方法检查背景颜色。但我无法做到。我在函数中使用查询块但它没有工作。
function elemanSurukleBasla(ev) {
ev.dataTransfer.setData("text", ev.target.id);
ID = ev.target.id;
//alert(ev.target.id);
}
function elemanSuruklenior(ev) {
ev.preventDefault();
}
function elemanBirakildi(ev) {
ev.preventDefault();
var veri = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(veri));
var deniyoruz = "hedef" + ID;
}
function kontrol(){
if (ev.target.id == deniyoruz) {
document.getElementById("hedef" + ID).style.backgroundColor = "green";
} else {
document.getElementById(ev.target.id).style.backgroundColor = "red";
}
}

<div id="secenekler"><br>
<div id="logo" class="soru_css" ondragover="elemanSuruklenior(event)" ondrop="elemanBirakildi(event)">
<img id="1" name="secenek1" class="secenek1" src="https://www.w3.org/html/logo/downloads/HTML5_Logo_256.png" width="50" height="50" draggable="true" draggable="true" ondragstart="elemanSurukleBasla(event)" data-target="1">
</div>
<div id="logo" ondragover="elemanSuruklenior(event)" ondrop="elemanBirakildi(event)">
<img id="2" name="secenek2" class="secenek2" src="https://vignette.wikia.nocookie.net/howtoprogram/images/a/a9/CSS3.png/revision/latest/scale-to-width-down/342?cb=20130422012035" width="50" height="50" draggable="true" ondragstart="elemanSurukleBasla(event)" data-target="2" >
</div>
<div id="logo" ondragover="elemanSuruklenior(event)" ondrop="elemanBirakildi(event)" >
<img id="3" name="secenek3" class="secenek3" src="https://upload.wikimedia.org/wikipedia/commons/d/dc/Javascript-shield.png" width="50" height="50" draggable="true" ondragstart="elemanSurukleBasla(event)" data-target="3">
</div>
</div>
<div id="sorular"> Sorular <br>
<div id="hedef2" name="hedef1" ondragover="elemanSuruklenior(event)" ondrop="elemanBirakildi(event)" data-target="2"></div> <br>
<div id="hedef1" name="hedef2" ondragover="elemanSuruklenior(event)" ondrop="elemanBirakildi(event)" data-target="1"></div> <br>
<div id="hedef3" name="hedef3" ondragover="elemanSuruklenior(event)" ondrop="elemanBirakildi(event)" data-target="3"></div><br>
</div>
<br>
<button type="button" onclick="kontrol()">KONTROL</button>
<br>
&#13;
如何使用JavaScript中的按钮检查背景?
答案 0 :(得分:-1)
尝试:
<button onclick="myFunc()">Click</button>
在你的js中:
function myFunc(){
return document.getElementById('myDiv').style.backgroundColor == 'black';
}