我在this.css的事件处理程序上收到错误,由于某种原因,我无法将这些图像分配到单元格的背景。

谢谢!

$(document).ready(function(){
  var turn = 0;

  $("#cell11 , #cell12, #cell13, #cell21, #cell22, #cell23, #cell31, #cell32, #cell33 ")
    .click(
      function(){
        // alert("click");
        var cell = this;
          if(turn=== 0){

            cell.css("background", "url(images/o.png");
            turn = 1 ;

          }else{
            cell.css("background", "url(images/x.png");
            turn = 0;
          }
      }
    );

});

最佳答案

您需要在this上实例化jQuery类:

var cell = $(this);




另外,按照in comments所述纠正拼写错误:

if( turn === 0){
    cell.css("background", "url(images/o.png)");
    turn = 1 ;
}
else {
    cell.css("background", "url(images/x.png)");
    turn = 0;
}

10-06 09:25