Javascript Knockout JS,在可观察数组中更改值时如何更改样式属性

Javascript Knockout JS,在可观察数组中更改值时如何更改样式属性,javascript,html,css,foreach,knockout.js,Javascript,Html,Css,Foreach,Knockout.js,哟,我正在尝试构建一个小游戏,其中一个包含6个子数组的数组,每个子数组包含7个值,生成一个国际象棋场 当您单击其中一个字段时,它应该将颜色从白色更改为红色或从白色更改为黄色。颜色属性取决于数据数组中写入的值(与嵌套div标记中的css绑定一起使用) 数组“data”(像矩阵一样构建)如下:[[0,0,1,2,0,2,1],[0,0,0,0,2,2,1]等] 在HTML中,我使用一个“forEach”循环将数组的值绑定到div标记,并生成chess字段-总共42个字段(6*7=42) 现在我得

哟,我正在尝试构建一个小游戏,其中一个包含6个子数组的数组,每个子数组包含7个值,生成一个国际象棋场

当您单击其中一个字段时,它应该将颜色从白色更改为红色或从白色更改为黄色。颜色属性取决于数据数组中写入的值(与嵌套div标记中的css绑定一起使用)

数组“data”(像矩阵一样构建)如下:[[0,0,1,2,0,2,1],[0,0,0,0,2,2,1]等]

在HTML中,我使用一个“forEach”循环将数组的值绑定到div标记,并生成chess字段-总共42个字段(6*7=42)


现在我得到了chess字段,当我在开始脚本之前更改“data”中的值时,颜色会相应地设置。当我点击一个字段时,数组值也会改变,但是div标签的颜色不会改变。 我该怎么做?

好的,我知道了

我用42个数字作为基数的字符串来创建矩阵。单击该字段时,矩阵中的值发生变化。虽然我还必须将矩阵改回一个简单的字符串,并将其与我最初创建国际象棋场的“创建者函数”一起重用

长话短说:

let result = self.addColor(id, self.data); 
//'result' is the new matrix with the changed value

let resultString = self.matrixToString(result()); 
//'resultString' is the conjoined array - a simple string made from 'result-matrix' 
//with a length of 42

let newMatrix = self.Spielfeld.creator(resultString);  
//now put that String into the "matrix-creator function" in order to create the
//updated matrix

self.data(newMatrix); 
//put the new matrix into the observable and your viewmodel should update
如果有人对淘汰赛了解得足够多,他们能解释为什么要这样做吗? 我读过关于休眠函数的书,这些函数不能访问可观察对象,因为访问不是“直接”发生的。这与此有关系吗

let result = self.addColor(id, self.data); 
//'result' is the new matrix with the changed value

let resultString = self.matrixToString(result()); 
//'resultString' is the conjoined array - a simple string made from 'result-matrix' 
//with a length of 42

let newMatrix = self.Spielfeld.creator(resultString);  
//now put that String into the "matrix-creator function" in order to create the
//updated matrix

self.data(newMatrix); 
//put the new matrix into the observable and your viewmodel should update