0

I'm trying to set up distinct listeners for a list of objects:

for(i=0;i<numInputs;i++) {
    var inPort = inputPorts.get(i);
    var portName = inPort.getProperty("name");
    inPort.shape.attr({ cursor: 'pointer' }).mouseover(function(e) { mouseOverInfo.text("PORT:: "+portName); }); 
}

What's happening right now is that every port object is only getting the very last element's name as its own. So when I mouse over, I get returned only the last object's name.

How to do this properly?

Thanks

4

1 に答える 1

1

ループ内のクロージャの問題を回避するには、次のような自己呼び出し関数を使用します。

for(i=0;i<numInputs;i++) {
  (function(i){
    var inPort = inputPorts.get(i);
    var portName = inPort.getProperty("name");
    inPort.shape.attr({ cursor: 'pointer' }).mouseover(function(e) { mouseOverInfo.text("PORT:: "+portName); }); 
  })(i)
}
于 2012-09-09T16:42:03.210 に答える