javascript - 如何检查充满 Jquery 对象的数组的值

标签 javascript jquery html loops

我正在制作一个井字棋游戏来娱乐,并且我正在尝试做一些不同的事情。

我目前正在尝试通过迭代使用 Jquery 抓取的存储 td 数组来检查获胜组合。

WIN_COMBINATIONS = [$("#square_0, #square_1, #square_2"), 
$("#square_6, #square_7, #square_8"),
$("#square_0, #square_3, #square_6"), 
$("#square_3, #square_4, #square_5"), 
$("#square_1, #square_4, #square_7"),
$("#square_2, #square_5, #square_8"), 
$("#square_0, #square_4, #square_8"), $("#square_6, #square_4, #square_2")]

所以,基本上,WIN_COMBINATIONS[0] 是一个获胜的组合。迭代并实际检查 Jquery 对象的 .html 的最佳方法是什么?

基本上,我想做这样的事情

if (WIN_COMBINATIONS[0].html = "X", "X", "X") {
        //do something here
}

感谢您的帮助!

最佳答案

WIN_COMBINATIONS.forEach(function(combination){
    if(combination.map(function(){return $(this).text()}).toArray().join("") == "XXX") { 
        console.log("winning combination")   
    } 
})   

关于javascript - 如何检查充满 Jquery 对象的数组的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51070672/

相关文章:

javascript - 不明白错误

php - 如何使用 jQuery 在屏幕上绘制字母

Html div 边框超出视口(viewport)

javascript - Jest 中从未调用图像 onLoad 处理程序

javascript - 在 Node.js 中 trim 和连接音频文件

javascript - 访问命名空间变量的更快方法?

jquery - 悬停以更改背景图像而不是背景颜色

javascript - JavaScript发送消息后如何修改textarea的设计?

javascript - 如何在 JavaScript 中迭代 JSON 对象并用它创建表?

javascript - 如何将显示的当前时间保存在数据库表中