javascript如何识别不同的目标?

时间:2014-10-24 17:32:04

标签: javascript html5

我一直试图获得六张图片来评估"正确"或"错误"取决于他们拖动到哪个图像。当放到sprite1上时,苹果,番石榴和梨子(.png)都应该评估正确,而菠萝,李子和紫色在放到sprite2时都应该评估正确。这说他们也应该评估错误,当被丢弃到不正确的精灵。

我遇到的问题是,他们所有人都要先评估是正确还是错误,这取决于先将精灵滴在精灵上。它不会识别何时将水果放到不正确的精灵身上,而且我不确定要添加什么来实现这一目标?

问题图:http://i.imgur.com/u9oH5Mo.png

<div id="answerBoxes">
<div id="target1" ondrop="drop(event)" ondragover="allowDrop(event)">
<img src="img/sprite1.png"/>
</div>
<div id="target2" ondrop="drop(event)" ondragover="allowDrop(event)">
<img src="img/sprite2.png"/>
</div>
</div> <!-- end of sprite's -->
<div id="whatsWhatContent">
<div id="answers">
<div id="answer1Box" ondrop="drop(event)" ondragover="allowDrop(event)">
   <img id="answer1" src="img/apple.png"  draggable="true"    
ondragstart="drag(event)"/></div>

<div id="answer2Box" ondrop="drop(event)" ondragover="allowDrop(event)">
   <img id="answer2" src="img/guava.png"  draggable="true" 
ondragstart="drag(event)"></div>

<div id="answer3Box" ondrop="drop(event)" ondragover="allowDrop(event)">
  <img id="answer3" src="img/pear.png"  draggable="true" 
ondragstart="drag(event)"></div>

  <div id="answer4Box" ondrop="drop(event)" ondragover="allowDrop(event)">
  <img id="answer4" src="img/pineapple.png"  draggable="true" 
ondragstart="drag(event)">

  <div id="answer5Box" ondrop="drop(event)" ondragover="allowDrop(event)">
  <img id="answer5" src="img/plum.png"  draggable="true" 
ondragstart="drag(event)">

  <div id="answer6Box" ondrop="drop(event)" ondragover="allowDrop(event)">
  <img id="answer6" src="img/purple.png"  draggable="true" 
ondragstart="drag(event)">

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>

function allowDrop(ev){
ev.preventDefault();
}

function drag(ev){
ev.dataTransfer.setData("content", ev.target.id);
}

function drop(ev){
ev.preventDefault();
var image = ev.dataTransfer.getData("content");
ev.target.appendChild(document.getElementById(image));

if($('#target1').find('#answer1').length == 1)
{
alert("CORRECT!");
} 
else if ($('#target2').find('#answer2').length == 1)
{
alert("Wrong.");
}
else {
alert("Wrong!");
}
}

0 个答案:

没有答案