为什么不能用这个?http://www.imooc.com/video/3233
function getclsname(clsname,parent){ var oparent=parent?document.getElementById(parent):document, eles=[], elements=oparent.getElementsByTagName('*'); for(var i=0;i<elements.length;i++){ if(elements[i].className==clsname){ eles.push(elements[i]); } } return eles; } function getstyle(ele,sty){ if(ele.currentStyle){ return ele.currentStyle[sty]; }else{ return window.getComputedStyle(ele,null)[sty]; } } window.onload = function () { var lis=document.getElementsByTagName('li'); //console.log(getclsname('toptitle',null)[0]); var div_1=getclsname('toptitle',null)[0];//document.getElementsByTagName('ul')[0].getElementsByTagName('div')[0], wd=parseInt(getstyle(div_1,'height')); for(var i=0;i<lis.length;i++){ lis[i].onmouseover=function(){ this.getElementsByTagName('div')[0].style.display='block'; this.getElementsByTagName('span')[0].className='my_span'; var a=this.getElementsByTagName('div')[0].offsetHeight; //var a=this.getclsname('submenu',null)[0]; console.log(a); } lis[i].onmouseleave=function(){ this.getElementsByTagName('div')[0].style.display='none'; this.getElementsByTagName('span')[0].className=''; } var h=i*parseInt(getstyle(lis[i],'height'))+wd; } }
这一段的,this.getclsname('submenu',null)[0];,
这句会报错:Uncaught TypeError: this.getclsname is not a function,
如果换成了,this.getELementsByTagName('div')[0]就没有问题呢?
非常感谢!