如何在DIV上的所有ID点击时显示最后一个ID

时间:2015-11-05 09:55:37

标签: javascript html

我是javascript的新手。 我想做迷你游戏测验。但坚持这个逻辑。

试试看下面的代码,,, 我想替换div ID"错误的gameOne"使用div ID" correct-gameOne" 点击所有div ID wrong1,wrong2,wrong3,wrong4,

我已尝试制作它,但点击一个ID"错误的1"或其他,div ID" correct-gameOne"是显示。我不想这样。 请给我修复我的脚本如何制作它。 对不起,我的英语不好。 非常感谢:脸红:



$(function() {
  $('#wrong1, #wrong2, #wrong3, #wrong4').click(function() {
    $(this).animate({
      opacity: 0,
      top: 100,
    }, 500);

    var divs = ['#wrong1, #wrong2, #wrong3, #wrong4'];

    if ($(this).next(divs).length === 1)
      $('#wrong-gameOne').eq(0).hide();
    else
      $('#correct-gameOne').eq(0).show();
  });
});

<img id="correct-gameOne" class="rightAnswer" src="http://authentic-scandinavia.com/system/images/tours/photos/10/thumbnail.jpg" data-current-game="1" data-next-game="2" style="display:none;" />
<img id="wrong-gameOne" class="wrongAnswer" src="http://authentic-scandinavia.com/system/images/tours/photos/125/thumbnail.jpg" data-current-game="1" data-next-game="2" />

<div id="wrong1" class="no-bottom">
  <label>TEXT 1</label>
</div>

<div id="wrong2" class="no-bottom">
  <label>TEXT 2</label>
</div>

<div id="wrong3" class="no-bottom">
  <label>TEXT 3</label>
</div>

<div id="wrong4" class="no-bottom">
  <label>TEXT 4</label>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

请尝试此代码。

 $(function () {
    var divs = ["wrong1", "wrong2", "wrong3", "wrong4"];
    $('#wrong1, #wrong2, #wrong3, #wrong4').click(function () {
        $(this).animate({
            opacity: 0,
           top: 100,
         }, 500);
    divs.splice(divs.indexOf($(this).prop("id")),1)
    if(divs.length == 0){
        $('#wrong-gameOne').eq(0).hide();
       $('#correct-gameOne').eq(0).show();
    }                
  });
});

希望这会对你有所帮助。

Fiddle