js this

来源:互联网 发布:手机看图纸软件 编辑:程序博客网 时间:2024/06/14 20:12
js 中的this指的是事件的产生对象。即getElementById的到的对象等一样。比如做一个鼠标移动到不同按钮面板切换效果通过鼠标停留到的span中的值来判断鼠标移动到哪里,应该在给span中添加的事件onmouseover()中传一个参数this。这样在js的onmouseover(obj)时间中接受这个对象参数,用obj.innerHTML来判断是滑过哪个span,如果添加事件时没有传参数this,那么这个函数里的this.innerHTML就得不到span中的值,因为此时的this指的是window而不是span。面板切换效果代码示例:html: <span OnMouseOver="action(this)"><br/>西</span><br/><br/>        <span OnMouseOver="action(this)"><br/></span><br/><br/>        <span OnMouseOver="action(this)"><br/></span>
js: function action(obj)  {     switch (obj.innerHTML)     {        case '找<br>西':span[53].style.background='red';span[54].style.background='gray';span[55].style.background='gray';div1.style.display='block'; div2.style.display='none';div3.style.display='none';break;        case '减<br>肥':span[54].style.background='red';span[53].style.background='gray';span[55].style.background='gray';div2.style.display='block'; div1.style.display='none';div3.style.display='none';break;        case '发<br>到':span[55].style.background='red';span[53].style.background='gray';span[54].style.background='gray';div3.style.display='block'; div2.style.display='none';div1.style.display='none';break;     }  }
0 0