jquery each中包含switch标签判断错误跳过each循环

$('li').each(function(){
    var isbool=true;
    switch($(this).attr('data-type')){
        case 'a':{
            //如果当前li标签下不存在p标签,则跳出
            if($(this).find('p').length==0){
                isbool=false;
            }                                   
            break;
        }
    }
    if(isbool==false){
        return false;
    }
});
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用jQuery编写俄罗斯方块的示例代码: ```html <!DOCTYPE html> <html> <head> <title>Tetris Game</title> <style type="text/css"> #game-board { width: 300px; height: 600px; border: 1px solid black; margin: 0 auto; position: relative; } .block { width: 30px; height: 30px; border: 1px solid black; position: absolute; } </style> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script type="text/javascript"> $(document).ready(function() { var gameBoard = $('#game-board'); var blockWidth = parseInt(gameBoard.css('width')) / 10; var blockHeight = parseInt(gameBoard.css('height')) / 20; var currentBlock = null; var currentBlockType = null; var currentBlockOrientation = null; var currentBlockPosition = null; var currentBlockTimer = null; var score = 0; var scoreBoard = $('#score-board'); var highScore = localStorage.getItem('tetris-high-score') || 0; var highScoreBoard = $('#high-score-board'); highScoreBoard.text(highScore); function createBlock(type, orientation, position) { var block = $('<div class="block"></div>'); block.css({ 'width': blockWidth + 'px', 'height': blockHeight + 'px', 'top': position[0] * blockHeight + 'px', 'left': position[1] * blockWidth + 'px' }); block.addClass(type + '-' + orientation); gameBoard.append(block); return block; } function moveBlockDown() { var newPosition = [currentBlockPosition[0] + 1, currentBlockPosition[1]]; if (isValidPosition(currentBlockType, currentBlockOrientation, newPosition)) { currentBlock.css('top', newPosition[0] * blockHeight + 'px'); currentBlockPosition = newPosition; } else { clearInterval(currentBlockTimer); currentBlockTimer = null; checkForCompletedRows(); createNewBlock(); } } function isValidPosition(type, orientation, position) { var blocks = getBlocksForTypeAndOrientation(type, orientation); for (var i = 0; i < blocks.length; i++) { var row = position[0] + blocks[i][0]; var col = position[1] + blocks[i][1]; if (row < 0 || row >= 20 || col < 0 || col >= 10) { return false; } if ($('#game-board .block[row="' + row + '"][col="' + col + '"]').length > 0) { return false; } } return true; } function getBlocksForTypeAndOrientation(type, orientation) { var blocks = []; switch (type) { case 'i': switch (orientation) { case 0: case 2: blocks = [[0, 0], [1, 0], [2, 0], [3, 0]]; break; case 1: case 3: blocks = [[0, 0], [0, 1], [0, 2], [0, 3]]; break; } break; case 'j': switch (orientation) { case 0: blocks = [[0, 0], [1, 0], [1, 1], [1, 2]]; break; case 1: blocks = [[0, 2], [1, 2], [2, 2], [2, 1]]; break; case 2: blocks = [[2, 0], [2, 1], [1, 1], [0, 1]]; break; case 3: blocks = [[0, 0], [0, 1], [1, 1], [2, 1]]; break; } break; case 'l': switch (orientation) { case 0: blocks = [[0, 2], [1, 0], [1, 1], [1, 2]]; break; case 1: blocks = [[0, 1], [1, 1], [2, 1], [2, 2]]; break; case 2: blocks = [[2, 0], [1, 0], [1, 1], [1, 2]]; break; case 3: blocks = [[0, 1], [1, 1], [2, 1], [0, 0]]; break; } break; case 'o': blocks = [[0, 0], [0, 1], [1, 0], [1, 1]]; break; case 's': switch (orientation) { case 0: case 2: blocks = [[0, 1], [0, 2], [1, 0], [1, 1]]; break; case 1: case 3: blocks = [[0, 1], [1, 1], [1, 2], [2, 2]]; break; } break; case 't': switch (orientation) { case 0: blocks = [[0, 1], [1, 0], [1, 1], [1, 2]]; break; case 1: blocks = [[0, 1], [1, 1], [2, 1], [1, 2]]; break; case 2: blocks = [[1, 0], [0, 1], [1, 1], [2, 1]]; break; case 3: blocks = [[0, 1], [1, 0], [1, 1], [2, 1]]; break; } break; case 'z': switch (orientation) { case 0: case 2: blocks = [[0, 0], [0, 1], [1, 1], [1, 2]]; break; case 1: case 3: blocks = [[0, 2], [1, 1], [1, 2], [2, 1]]; break; } break; } return blocks; } function createNewBlock() { var types = ['i', 'j', 'l', 'o', 's', 't', 'z']; var type = types[Math.floor(Math.random() * types.length)]; var orientation = Math.floor(Math.random() * 4); var position = [0, Math.floor(Math.random() * 7)]; currentBlock = createBlock(type, orientation, position); currentBlockType = type; currentBlockOrientation = orientation; currentBlockPosition = position; currentBlockTimer = setInterval(moveBlockDown, 500); } function checkForCompletedRows() { for (var row = 0; row < 20; row++) { var blocksInRow = $('#game-board .block[row="' + row + '"]'); if (blocksInRow.length === 10) { blocksInRow.remove(); score += 10; scoreBoard.text(score); if (score > highScore) { highScore = score; highScoreBoard.text(highScore); localStorage.setItem('tetris-high-score', highScore); } $('#game-board .block').each(function() { var blockRow = parseInt($(this).attr('row')); var blockCol = parseInt($(this).attr('col')); if (blockRow < row) { $(this).css('top', (blockRow + 1) * blockHeight + 'px'); $(this).attr('row', blockRow + 1); } }); row--; } } } $(document).keydown(function(event) { if (currentBlockTimer !== null) { switch (event.which) { case 37: // left arrow var newPosition = [currentBlockPosition[0], currentBlockPosition[1] - 1]; if (isValidPosition(currentBlockType, currentBlockOrientation, newPosition)) { currentBlock.css('left', newPosition[1] * blockWidth + 'px'); currentBlockPosition = newPosition; } break; case 38: // up arrow var newOrientation = (currentBlockOrientation + 1) % 4; if (isValidPosition(currentBlockType, newOrientation, currentBlockPosition)) { currentBlock.removeClass(currentBlockType + '-' + currentBlockOrientation); currentBlock.addClass(currentBlockType + '-' + newOrientation); currentBlockOrientation = newOrientation; } break; case 39: // right arrow var newPosition = [currentBlockPosition[0], currentBlockPosition[1] + 1]; if (isValidPosition(currentBlockType, currentBlockOrientation, newPosition)) { currentBlock.css('left', newPosition[1] * blockWidth + 'px'); currentBlockPosition = newPosition; } break; case 40: // down arrow moveBlockDown(); break; case 32: // space bar var newOrientation = (currentBlockOrientation + 1) % 4; if (isValidPosition(currentBlockType, newOrientation, currentBlockPosition)) { currentBlock.removeClass(currentBlockType + '-' + currentBlockOrientation); currentBlock.addClass(currentBlockType + '-' + newOrientation); currentBlockOrientation = newOrientation; } break; case 13: // enter key if (currentBlockTimer !== null) { clearInterval(currentBlockTimer); currentBlockTimer = null; } else { currentBlockTimer = setInterval(moveBlockDown, 500); } break; } } }); createNewBlock(); }); </script> </head> <body> <div id="game-board"></div> <div id="score-board">0</div> <div id="high-score-board"></div> </body> </html> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值