我有关于eval()的问题,我正在验证用户使用对象javascript提交的数据..
E.g。
for(var i=0;i<count;i++){
switch(caseType){
case: 'Group':
### it will execute the ff code
### it will call MustChecked function
if(!eval(this.MustChecked(getAttribute('class'), getAttribute('min'), getAttribute('max')))) {
####this will alert all the errors..
this.AddError(i, getAttribute("msg"));
}
}
}
从这段代码中,它会提醒所有错误......这段代码不适用于IE8和IE7 ......任何人都知道为什么不在IE8和IE7中工作?
更新:经过检验的功能
MustChecked : function(name, min, max) { var groups = document.getElementsByClassName(name); alert(groups); //console.log(name + ":" + groups.length); var hasChecked = 0; min = min || 1; max = max || groups.length; for(var i=groups.length-1;i>=0;i--) if(groups[i].checked) hasChecked++; return min <= hasChecked && hasChecked <= max; },
更新V1.1
我试过这个
getElementsByClassName : function (className) {
if (document.getElementsByClassName) {
return document.getElementsByClassName(className);
}
else {
return document.querySelectorAll('.' + className);
}
},
这项工作在IE8中,但在IE7中没有...运气好吗?
答案 0 :(得分:3)
IE7 / 8不支持getElementsByClassName
方法。您可以在MustChecked
函数中使用它。
不支持,因为“不存在”。
有垫片可用。快速谷歌搜索将给你一些结果。或者您可以使用Sizzle或仅使用querySelectorAll
垫片,它们更通用。
如果您想知道某项功能是否受支持,那么一个好的资源就是caniuse.com。