jQuery - using inArray() to find index of jQuery object

后端 未结 7 1216
粉色の甜心
粉色の甜心 2021-01-19 02:09

I have a few divs that I\'d like to put into an array.

When I try to use jQuery.inArray(), my div (as a jQuery object) isn\'t found. Why not?

var my         


        
相关标签:
7条回答
  • 2021-01-19 02:48

    You're better off creating an array of ids. When it you roll, you can then see if that id is in your array, and then move forward.

    var possiblePositions = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
    
    function randomSpin(sides) {
     return Math.floor(Math.random() * (sides || 6) ) + 1;
    }
    var $currentPiece = $('piece.active');
    var currentSpot = $currentPiece.attr('spotPosition');
    var spin = randomSpin(6) + randomSpin(6);
    var nextSpot = currentSpot + spin;
    
    if (possiblePositions.indexOf(nextSpot)) {
        $('#div' + nextSpot).append($currentPiece);
    }
    
    0 讨论(0)
提交回复
热议问题