为什么我的jquery对象改变了上下文?

时间:2013-12-10 21:49:27

标签: javascript jquery jquery-context

我在mousedown事件中比较了两个元素但由于context属性它们是假的:

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
        }
    }
});

为什么上下文改变了?

0 个答案:

没有答案