看了好些同学的代码,为什么都喜欢写Elements的获取方法,还得多写个if判断呢?用className获取到需要的subNav就够了吧?
clearInterval(this.time);
var sn=this.getElementsByClassName('subNav');
this.time = setInterval(function(){
if(sn[0].offsetHeight <= 120){
sn[0].style.height = sn[0].offsetHeight+8+'px';
}
},30);这是我写的。下面是别人的。
var oSubNav=this.getElementsByTagName('ul')[0];
if(oSubNav){
var This=oSubNav;
clearInterval(This.time);
This.time=setInterval(function(){
This.style.height=This.offsetHeight+16+"px";
if(This.offsetHeight>=120)
clearInterval(This.time);
},30)
}多套了一个if不是更繁琐了吗?
另外,用className来获取元素是否比Elements获取的方法要浪费性能啊?