2

我在IE7中遇到了一些JS问题。我正在测试以查看某个对象是否具有分配的className(可能是来自DOM的HTMLElement对象)。现在Internet Explorer 7 - Javascript'undefined'not testing

,在Firefox测试的网页告诉我,是的,变量是不确定的(我所有的测试下面做警报()。

在IE中,没有一个测试通过,该变量被所分配的最后IF语句,并在最后警报()IE夹头的“类名是空或不是对象”错误的基础上,fn_note.className声明

下面的代码:

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

请让我知道我做错了什么,我知道它可能有些基本的东西,但我不能看到它的ATM。

在此先感谢。

+0

刚刚在IE7上测试过:http://jsbin.com/azoya/报告一个空字符串。 – strager 2009-02-26 02:29:33

+0

也在这里测试过它,空字符串以及 – eglasius 2009-02-26 08:36:33

回答

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

这应该是足够的(不要介意在正则表达式中破折号)。

6

我认为你从属性得到的不是一个字符串,而是一个'undefined'类型的对象。试试这个:

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