nas*_*sty 2 javascript firefox internet-explorer cross-browser internet-explorer-7
我在IE7中遇到了一些JS问题.我正在测试是否有某个对象分配了className(可能是来自DOM的HTMLElement对象).
现在,在Firefox中测试页面告诉我,是的,变量是未定义的(我的所有测试都在下面做Alert().
在IE中,没有一个测试通过,变量在最后一个IF语句中被分配,并且在最后一个Alert()IE中,根据fn_note.className语句,chucks"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);
Run Code Online (Sandbox Code Playgroud)
请让我知道我做错了什么.我知道它可能是基本的东西,但我只是看不到ATM.
提前致谢.
我认为你从属性得到的不是字符串,而是'undefined'类型的对象.试试这个:
if (typeof(kids[l].className) == 'undefined') {
Run Code Online (Sandbox Code Playgroud)