2010-06-03 6 views
0

XML 문서에서 데이터를 추출하여 일부 JavaScript 객체에 추가하는 'for'루프가 있는데, 루프가 실행될 때마다 특정 함수는 XML에서 검색되는 'typ'속성의 값에 따라 다릅니다.JavaScript, jQuery, XML - 알려지지 않은 이유로 if 문이 실행되지 않음

현재 XML의 데이터가 파싱되고 있으며 올바른 값을 생성하는 첫 번째 '경고'에 의해 입증됩니다.

그러나 'if'문 아래쪽에 'alert'줄이 실행되지 않아 결과적으로 호출해야하는 'ctyp3'함수를 테스트 할 수 없습니다.

어디로 잘못 갔습니까?

     for (j=0; j<arrayIds.length; j++) 
        { 
        $(xml).find("C[ID='" + arrayIds[j] + "']").each(function(){ 
         // pass values 
         questions[j] = { 
          typ: $(this).attr('typ'), 
          width: $(this).find("I").attr('wid'), 
          height: $(this).find("I").attr('hei'), 
          x: $(this).find("I").attr('x'), 
          y: $(this).find("I").attr('x'), 
          baC: $(this).find("I").attr('baC'), 
          boC: $(this).find("I").attr('boC'), 
          boW: $(this).find("I").attr('boW') 
         } 

         alert($(this).attr('typ')); 

         if ($(this).attr('typ') == '3') 
         { 
          ctyp3(x,y,width,height,baC); 
          alert('pass'); 
         } else { 
          // Add here 
          alert('fail'); 
         } 
        }); 
        } 

편집 : 귀하의 제안 사항 후 나는 ctyp3 함수에 주석을 달았고 if 문은 올바르게 실행되었습니다. 그때는 객체를 생성하려고 시도했습니다

     function ctyp3(x,y,width,height,baC) 
        { 
         alert('alsdjkfs'); 
         document.createElement('rect'); 
         this.x = x; 
         this.y = y; 
         this.width = width; 
         this.height = height; 
         this.fillcolor = baC; 
         svgTag.appendChild(this); 
        } 

과 HTML 요소, 내가 전에 완료하지 않은 무언가 : 여기

는 ctyp3 기능입니다.

+0

'ctyp3' 메소드가 실패했을 수 있습니다. 여기에 게시하거나 거기에서 디버그 할 수 있습니까? – sirhc

+0

'ctyp3'가 예외를 던지지 않는 것이 확실합니까? try/catch에서'ctyp3' 호출을 래핑하면 어떻게 될까요? – Pointy

답변

2

ctyp3 함수의 "this"는 사용자가 생각하는대로되지 않습니다. 당신은 변수에 document.createElement의 결과를 할당해야합니다 :

var r = document.createElement('rect'); 

그런 다음 대신 "이"의 "R"을 사용합니다. 마찬가지로 "svgTag"가 무엇인지도 분명하지 않습니다. 전역 변수가 아닌 경우 함수에 명시 적으로 전달해야합니다.

관련 문제