1
var jewels = new Array()
var index
var rand

var scard = [a1, a2, a3, b1, b2, b3, c1, c2, c3]

    function generateBoard() {

        for (var i = 0; i <= 8; i++) {
            jewels[i] = new Array();

            var index = Math.floor(Math.random() * scard.length)
            jewels[i] = scard[index]
            console.log("I:" + i + jewels[i])
            var yy = scard.indexOf(scard[index]);
            scard.splice(yy, 1);
        }
    }

    function drawBoard() {

        generateBoard()
        var html = '<div class=Game_Background>'

        for (var i = 0; i < 9; i++) {

            rand = Math.floor(Math.random() * jewels.length)
            html += '<div class="' + jewels[rand] + '" id="ship' + i + '" style="top:' + ((i * 12) + 135) + 'px; left:245px;" ></div>'
            jewels.splice(rand, 1);
        }
        var newarray = [a1, a2, a3, b1, b2, b3, c1, c2, c3]
    }

这里a1,a2,a3,b1,b2,b3,c1,c2,c3是css类。我使用newarray是因为我拼接了所有的数组项。现在假设ship2 id的元素是b3,b3定位在newarray[ 5]。我如何检查这个(id 元素位于数组中的哪个索引数中)?

4

1 回答 1

1
<script type="text/javascript">
var jewels = new Array()
var index
var rand

var scard = ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"];
var newarray = ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"];

drawBoard();



function drawBoard() {


    var html = '<div class=Game_Background>'

    for (var i = 0; i < 9; i++) {

        rand = Math.floor(Math.random() * scard.length);
        html += '<div class="' + scard[rand] + '" id="ship' + i + '" style="top:' + ((i * 12) + 135) + 'px; left:245px;" ></div>'
        alert(newarray.indexOf(scard[rand]));
        scard.splice(rand, 1);
    }
}

</script>
于 2013-05-13T09:37:14.930 回答