私の全体的な目的は、画像に基づいてテストを作成することです。しかし、私は早い段階で立ち往生しています。アイデアは、画像が表示され、ユーザーが正しい答えを選択するためにボタンをクリックしなければならないということです。これまでクリックしたボタンは、画像が別の画像にランダムに置き換えられます。私が抱えている問題は、イメージが新しいイメージによってランダムに置換されると、新しいイメージと同じdiv内のボタンが機能しないということです。したがって、ユーザーは最初のdivのボタンしか使用できず、このdivが置き換えられると、新しいrandom divを超えることはできません。私はフォーラムの初心者ですが、誰かが私を助けることができれば非常に感謝します。Javascript - divを別のランダムdivに置き換える(新しいdivでボタンが機能しない)
ここは私のhtmlです。ここで
<div id = 'suitcase'>
<img src ='https://upload.wikimedia.org/wikipedia/commons/c/ce/Suitcase_750x500.jpg' width = '400'/>
<ul> <button id = 'correct1'> suitcase </button> <button class = 'incorrect1'> backpack </button> <button class = 'incorrect1'> handbag </button> </ul>
</div>;
<div id = 'printer'>
<img src = 'http://cdn2.pcadvisor.co.uk/cmsdata/reviews/3598061/HP_Envy_5640_e-All-in-One.jpg' width = '400'/>
<ul> <button id = 'correct2'> printer </button> <button class = 'incorrect2'> photocopier </button> <button class = 'incorrect2'> computer </button> </ul>
</div>;
<div id = 'desk'>
<img src = 'https://www.directofficesupply.co.uk/components/com_virtuemart/shop_image/product/H4P3.jpg' width = '400'/>
<ul> <button class = 'incorrect3'> printer </button> <button class = 'correct3'> desk </button> <button class = 'incorrect3'> computer </button> </ul>
</div>
は私が間違っている?何をやっている私のJavascriptとjQuery
$(window).load(function() {
$("#printer").hide();
});
$(window).load(function() {
$("#desk").hide();
});
var image1 = document.getElementById("desk").innerHTML;
var image2 = document.getElementById("suitcase").innerHTML;
var image3 = document.getElementById("printer").innerHTML;
var imageList = [image1, image2, image3]
function getRandomImage() {
var random = imageList[Math.floor(Math.random() * imageList.length)];
return random;
}
var randomImage = getRandomImage();
$("#correct1").click(function() {
$(this).css({
"background": "blue"
});
setTimeout(function() {
$("#suitcase").html(randomImage);
}, 500);
$("#correct2").click(function() {
$(this).css({
"background": "blue"
});
setTimeout(function() {
$("#printer").html(randomImage);
}, 500);
});
$("#correct3").click(function() {
$(this).css({
"background": "blue"
});
setTimeout(function() {
$("#desk").html(randomImage);
}, 500);
});
});
$(".incorrect1").click(function() {
$(this).css({
"background": "red"
});
setTimeout(function() {
$("#suitcase").html(randomImage);
}, 500);
});
$("#correct2").click(function() {
$(this).css({
"background": "blue"
});
setTimeout(function() {
$("#printer").html(randomImage);
}, 500);
});
$(".incorrect2").click(function() {
$(this).css({
"background": "red"
});
setTimeout(function() {
$("#printer").html(randomImage);
}, 500);
});
$("#correct3").click(function() {
$(this).css({
"background": "blue"
});
setTimeout(function() {
$("#desk").html(randomImage);
}, 500);
});
$(".incorrect3").click(function() {
$(this).css({
"background": "red"
});
setTimeout(function() {
$("#desk").html(randomImage);
}, 500);
});
です!
このコードには、字下げが必要です。 – Jonathan
すべてのDIVで同じものを再利用するのではなく、複数のボタンを使用するという点はありますか?また、ボタンの名前だけであなたのコードを見ることで、人々は不正行為をする可能性があります。 – Chepech