如何在js中显示警报的数量

时间:2017-03-28 07:30:51

标签: javascript html

我在javascript中有一个序列游戏的代码。我正在匹配从一行到另一行的卡片。如果卡片匹配正确,那么它将显示来自var correctcards的警报。当我拖动行的最后一张卡片时,我想显示用户在成功消息中拖动的正确卡片的数量。

    var correctCards = 0;
var maxCards = 0;
$( init );

function init() {

  // Hide the success message
  $('#successMessage').hide();
  $('#successMessage').css( {
    left: '580px',
    top: '250px',
    width: 0,
    height: 0
  } );

  // Reset the game
  correctCards = 0;
  $('#cardPile').html( '' );
  $('#cardSlots').html( '' );

  // Create the pile of cards
  var numbers = [ 'j', 1, 3, 4, 5, 6, 7, 8, 9, 10 , 'a'];
  // Create the card slots
  var words = [ 'jack', 'one', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'as' ];
  // Count max card for success test
  maxCards = numbers.length;

  // Create the pile of shuffled cards by copy of numbers and sort randomly
  var numbersShuffled = numbers.slice(0).sort( function() { return Math.random() - .5 } );

  // Display words  
  for ( var i=0; i<words.length; i++ ) {
    $('<div>' + words[i] + '</div>').data( 'number', i ).appendTo( '#cardSlots' ).droppable( {
      accept: '#cardPile div',
      hoverClass: 'hovered',
      drop: handleCardDrop
    } );
  }

  // Display shuffled list of number
  for ( var i=0; i<numbersShuffled.length; i++ ) {
    // Get number (index in array) of suffled number
    var numberIndex = numbers.indexOf(numbersShuffled[i]);
    $('<div>'+ numbersShuffled[i] + '</div>').data( 'number', numberIndex ).attr( 'id', 'card'+i ).appendTo( '#cardPile' ).draggable( {
      containment: '#content',
      stack: '#cardPile div',
      cursor: 'move',
      revert: true
    } );
  }


}
function handleCardDrop( event, ui ) {
  var slotNumber = $(this).data( 'number' );
  var cardNumber = ui.draggable.data( 'number' );

  // If the card was dropped to the correct slot,
  // change the card colour, position it directly
  // on top of the slot, and prevent it being dragged
  // again

  if ( slotNumber == cardNumber ) {
    ui.draggable.addClass( 'correct' );
    ui.draggable.draggable( 'disable' );
    $(this).droppable( 'disable' );
    ui.draggable.position( { of: $(this), my: 'left top', at: 'left top' } );
    ui.draggable.draggable( 'option', 'revert', false );
    correctCards++;
  } 

  // If all the cards have been placed correctly then display a message
  // and reset the cards for another go

  if ( correctCards == maxCards ) {
    $('#successMessage').show();
    $('#successMessage').animate( {
      left: '380px',
      top: '200px',
      width: '400px',
      height: '100px',
      opacity: 1
    } );
  }

}
    #cardPile,#cardSlots {
  width: 40%;
  display: inline-block;
}
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>



    <div id="content">

      <div id="successMessage">
        <h2>You did it!</h2>
***here i want to display number of correct cards***
        <button onclick="init()">Play Again</button>
      </div>

      <div id="cardPile"> </div>
      <div id="cardSlots"> </div>

    </div>

2 个答案:

答案 0 :(得分:0)

使用id放置span标记或div,然后在javascript上执行此操作

  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>



<div id="content">

  <div id="successMessage">
    <h2>You did it!</h2>
<span id ="myDiv"></span>
    <button onclick="init()">Play Again</button>
  </div>

  <div id="cardPile"> </div>
  <div id="cardSlots"> </div>

</div>

var myDiv= document.getElementById('divID');

myDiv.innerHTML = myDiv.innerHTML + ' ' + correctCards++;

答案 1 :(得分:0)

在HTML中添加一个将为响应消息保留的元素:

  <div id="successMessage">
    <h2>You did it!</h2>
    <div id="numberOfCards"></div>
    <button onclick="init()">Play Again</button>
  </div>

在显示successMessage的逻辑中添加一行来操纵其innerText。

// javascript
document.getElementById("numberOfCards").innerText
// jquery
$("#numberOfCards").text()