且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery:选择器定位动态属性不工作

更新时间:2023-11-16 15:26:40

问题是你总是有约束力 data-color = blue 因为脚本启动时变量 activePlayer 是蓝色。

The problem is that you're always binding on data-color=blue because the variable activePlayer is 'blue' when the script starts.

您可以通过将事件绑定到这两种情况来解决它。
像这样:

You can solve it by binding an event to both cases. Like this:

$("#board").on("click", "[data-color='red']", function(){
        activeStone["x"] = $(this).attr("data-x");
        activeStone["y"] = $(this).attr("data-y");
        checkPossibleMoves(activeStone["x"],activeStone["y"], "red");
    });

     $("#board").on("click", "[data-color='blue']", function(){
        activeStone["x"] = $(this).attr("data-x");
        activeStone["y"] = $(this).attr("data-y");
        checkPossibleMoves(activeStone["x"],activeStone["y"], "blue");
    });

一个替代方法是没有 data-color 属性并在回调中检索它。

An alternative would be binding without a data-color attribute and retrieving it in your callback.