2013-02-22 3 views
0

그것은 이상한패스 인수

var animateFunction = function (i) { 
if (animBool) { 
    Fn.slideData(values(i)); // To fetch some data 
    util.animate.tweenAnimate(sliderWrapper, 0 , Math.ceil("-"+sliderWidth)); 
    animBool = 0; 
} else { 
    util.animate.tweenAnimate(sliderWrapper, Math.ceil("-"+sliderWidth) ,0); 
    animBool = 1; 
} 
}; 

for (i = 0; i < annotateArray.length; i++) { 
//To Remember the state of value "i" 
(function (i) { 
    //Custom Event Listener for fallback for IE 
    util.addEvent(annotateArray[i], "click", animateFunction(i), true); 
}(i)); 
} 

for (i = 0; i < annotateArray.length; i++) { 

    //Custom Remove Event Listener for fallback for IE 
    util.removeEvent(annotateArray[i], "click", animateFunction); 

} 

//Custom Bind Event Method 
util.addEvent = function (obj, evt, callback, capture) { 
    if (window.attachEvent) { 
     obj.attachEvent("on" + evt, callback); 
    } else { 
     if (!capture) { 
      capture = false; 
     } 
     obj.addEventListener(evt, callback, capture); 
    } 
}; 

내가 모든 요소에 동적으로 이벤트를 결합하려고하지만 혹 내가이 기능은 '아무튼 요소를 클릭 동작 예상대로 동작하지 않습니다.

답변

1

실제로는 undefined을 실제 콜백이 아닌 이벤트 처리기로 전달합니다. 여기 :

util.addEvent(annotateArray[i], "click", animateFunction(i), true); 

undefined을 반환하는 함수를 호출하고 있습니다. 함수 참조를 addEventListener에 전달해야합니다. 당신은 이미 당신의 루프에서 '나는 가치있는 상태'를 기억할 수있는 무언가를 가지고 있지만 올바르게 사용하지는 않을 것입니다. 흠

for (i = 0; i < annotateArray.length; i++) { 
    //To Remember the state of value "i" 
    (function (i) { 
     // Custom Event Listener for fallback for IE 
     util.addEvent(annotateArray[i], "click", function() {animateFunction(i)}, true); 
    }(i)); 
} 
+0

,하지만 난 참조 콜백 함수를 통과 한 이유는 ... u는 그럼 당신은 배열을 만들 수 있습니다 편집 코드 –

+0

의 조각을 확인 할 수있는 ... 그것도 이벤트를 바인딩을 해제해야합니다 그것은해야한다 ('function {) {animateFunction (i)} '과 같은) 모든 핸들러에 대한 참조를 저장합니다. 나중에 배열의 참조를 사용하여 바인딩을 해제 할 수 있습니다. – bfavaretto

+0

Thanks mate :) 마침내 배열을 만들고 모든 핸들러에 대한 참조를 저장해야했습니다. –