2

IE7의 일부 JS에 문제가 있습니다. 특정 객체에 className (DOM의 HTMLElement 객체)이 할당되었는지 확인하려고합니다.Internet Explorer 7 - Javascript 'undefined'가 테스트되지 않습니다.

이제 파이어 폭스에서 페이지를 테스트하는 것은 예, 변수가 정의되지 하더군요 (내 모든 시험은 아래의 경고을(). IE에서

는 시험 중 어느 것도 통과하지 변수는에 할당됩니다 문 IF, 마지막 경고 중 마지막() IE는이 fn_note.className 문에 따라 오류 "클래스 이름이 null 여부를 객체이다"척 여기

코드입니다 :.

 var fn_note; 
     var kids = area.childNodes; 
     for (var l = 0; l < kids.length; l++){ 
      //DEBUG check if the found var exists 
      if (kids[l].className == null){ 
       //then the className var doens't exist 
       alert ('the classsname for the following var is null: --'+kids[l]+'--'); 
      } 
      if (kids[l].className == undefined){ 
       //then the className var doens't exist 
       alert ('the classsname for the following var is undefined: --'+kids[l]+'--'); 
      }      
      if (kids[l].className == ''){ 
       //then the className var doens't exist 
       alert ('the classsname for the following var is an empty string: --'+kids[l]+'--'); 
      } 
      if (typeof kids[l].className === 'undefined'){ 
       //then the className var doens't exist 
       alert ('the classsname for the following var is NEW TYPEOF TEST: --'+kids[l]+'--'); 
      }      

      if (kids[l].className == 'fn-note') { /* (/fn-note$/).test(kids[l].className) IE doesn't really like regex. por supuesto */ 
       //we have found the div we want to hide 
       fn_note = kids[l];      
      } 
     }      
     alert('the clicked on className is '+area.className+'name of the found div is '+fn_note.className); 

알려 주시기 바랍니다 내가 뭘 잘못하고 있는지. 아마 그 중 일부는 알고있다. 기본적이지만 현금으로 만 볼 수는 없습니다.

미리 감사드립니다.

+0

IE7에서 막 테스트되었습니다. http://jsbin.com/azoya/ 빈 문자열을보고합니다. – strager

+0

또한 여기에서 빈 문자열을 테스트했습니다. – eglasius

답변

1

를 지금까지 내가 당신이 정말 childNodes에 컬렉션에서 클래스 이름 'FN-노트'와 childNode에있을 경우이 알고 싶은 유일한 일을 볼 수 있습니다.

for (var l = 0; l < kids.length; l++){ 
    if (kids[l] 
     && kids[l].className 
     && kids[l].className.match(/fn\-note$/i)) { 
     fn_note = kids[l]; 
    } 
} 

이 충분해야한다 (마음이 정규 표현식에서 대시를 탈출 할) : 그래서 좀 더 rigourous 시험을한다.

6

당신이 속성에서 얻는 것은 문자열이 아니라 'undefined'유형의 객체라고 생각합니다. 이 시도 :

if (typeof(kids[l].className) == 'undefined') { 
+0

해당 코드는 이미 예제에 있습니다. – pope

+0

글쎄요, 정확하게는 아닙니다. 이 코드는 전혀 필요하지 않은 유형 문자열에 대한 추가 유형 검사를 수행하며 실제로 문제를 일으킬 수 있습니다. 리터럴 문자열 유형이 있고 String 객체가 있으며 동일한 유형으로 평가되지 않을 수 있습니다. – Guffa

+0

도움을 주셔서 감사합니다. –