<script> function fx(divId,divName,zDivCount){ console.log("WeiCot Java Script Test \r\n This $this-> \r\n WWW.WEICOT.COM"); for(i=0;i<=zDivCount;i++) { console.warn("NONE "+divName+i) //将所有的层都隐藏 } console.warn("BLOCK "+divName+divId); //默认指向全局 console.log(this.document === document);//true console.log(this===window); //true this.a=37; console.log(window.a); //37 console.log(this.value); //undefined //对于不是 事件处理 函数的场合 //可以使用 apply 或 call //来改变 this的指向 fthis.call(WFS); //www.weicot.com } var WFS={ name:'WFS', age:1, WebSite:'www.weicot.com' }; function fthis(){ console.log(this.WebSite); } function sw(id){ //switch 选项 switch (id){ case 'd1': document.getElementById(id).style.background="blue"; break; case 'd2': document.getElementById(id).style.background="blue"; break; case 'd3': document.getElementById(id).style.background="blue"; break; } } </script> <button name="test" value="IsValue" onclick="fx('0','DIVN_',1)" >Test</button> <div class="sw" id="d1" onclick="sw(this.id)";>test div</div> <div class="sw" id="d2" onclick="sw(this.id)";>test div</div> <div class="sw" id="d3" onclick="sw(this.id)";>test div</div> <style> .sw{ background:red; } </style>
参考地址
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this