0

我正在比较 mousedown 事件的两个元素,但由于上下文属性,它们是错误的:

console.log(this, opt.selectedNode);
[circle, prevObject: x.fn.x.init[1], context: undefined, jquery: "2.0.3", constructor: function, init: function…]
[circle, prevObject: x.fn.x.init[3], context: document, jquery: "2.0.3", constructor: function, init: function…]

这就是我在 mousedown 上所做的:

$("circle").ondrag({
    start:function(){
        //this is a jQuery object
        console.log(this==opt.selectedNode); //true the first time, false the second time it's executed
        console.log(this[0]==opt.selectedNode[0]); //always true
        if (this==opt.selectedNode) { //select other node
            $(".selectedNode").removeNSClass("selectedNode");
            if (!point.isFirst()) {
                opt.selectedNode = $("circle").bydata('point',pointarray[pointarray.indexOf(point)-1]).addNSClass("selectedNode"); //if there is a node before
            } else if (!point.isLast()) {
                opt.selectedNode = $("circle").bydata('point',pointarray[pointarray.indexOf(point)+1]).addNSClass("selectedNode"); //if there is a node after
            } else opt.selectedNode = null; //last point, select nothing
        }
    }
});

为什么上下文发生了变化?

4

0 回答 0