1. JS面向对象基础
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>
无标题文档
</title>
<script>
function createPerson(name, qq) //构造函数
{
//系统偷偷替咱们做:
//var this=new Object();
//加工
this.name=name;
this.qq=qq;
this.showName=function ()
{
alert('我的名字叫:'+this.name);
};
this.showQQ=function ()
{
alert('我的QQ号:'+this.qq);
};
//也会偷偷做一些:
//return this;
}
var obj=new createPerson('blue', '258248832');
var obj2=new createPerson('张三', '45648979879');
obj.showName();
obj.showQQ();
//alert(obj.showName==obj2.showName);
</script>
</meta>
</head>
<body>
</body>
</html>
普通函数中的this是指向window的原因:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>
无标题文档
</title>
<script>
window.show=function ()
{
alert(this);
};
show();
</script>
</meta>
</head>
<body>
</body>
</html>
2. JS面向对象实例
通过选显卡实例玩转JavaScript里的this,第5个为正确版
1.
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1 input {background:white;}
#div1 input.active {background:yellow;}
#div1 div {width:200px; height:200px; background:#CCC; display:none;}
</style>
<script>
window.onload=function ()
{
var oDiv=document.getElementById('div1');
var aBtn=oDiv.getElementsByTagName('input');
var aDiv=oDiv.getElementsByTagName('div');
for(var i=0;i<aBtn.length;i++)
{
aBtn[i].index=i;
aBtn[i].onclick=function ()
{
for(var j=0;j<aBtn.length;j++)
{
aBtn[j].className=''
aDiv[j].style.display='none';
}
this.className='active';
aDiv[this.index].style.display='block';
// console.log(i)
};
}
};
</script>
</head>
<body>
<div id="div1">
<input class="active" type="button" value="aaa" />
<input type="button" value="bbb" />
<input type="button" value="ccc" />
<div style="display:block;">aaa</div>
<div>dfsadf</div>
<div>erwqerwe</div>
</div>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1 input {background:white;}
#div1 input.active {background:yellow;}
#div1 div {width:200px; height:200px; background:#CCC; display:none;}
</style>
<script>
var aBtn=null;
var aDiv=null;
window.onload=function ()
{
var oDiv=document.getElementById('div1');
aBtn=oDiv.getElementsByTagName('input');
aDiv=oDiv.getElementsByTagName('div');
for(var i=0;i<aBtn.length;i++)
{
aBtn[i].index=i;
aBtn[i].onclick=fnClick;
}
};
function fnClick()
{
console.log('fnClick', this)
for(var j=0;j<aBtn.length;j++)
{
aBtn[j].className=''
aDiv[j].style.display='none';
}
this.className='active';
aDiv[this.index].style.display='block';
// console.log(i)
}
</script>
</head>
<body>
<div id="div1">
<input class="active" type="button" value="aaa" />
<input type="button" value="bbb" />
<input type="button" value="ccc" />
<div style="display:block;">aaa</div>
<div>dfsadf</div>
<div>erwqerwe</div>
</div>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1 input {background:white;}
#div1 input.active {background:yellow;}
#div1 div {width:200px; height:200px; background:#CCC; display:none;}
</style>
<script>
window.onload=function ()
{
new TabSwitch('div1');
};
function TabSwitch(id)
{
var oDiv=document.getElementById(id);
this.aBtn=oDiv.getElementsByTagName('input');
this.aDiv=oDiv.getElementsByTagName('div');
for(var i=0;i<this.aBtn.length;i++)
{
this.aBtn[i].index=i;
this.aBtn[i].onclick=this.fnClick;
}
console.log('aa',this.fnClick)
// console.log('tabswitch', this)
};
TabSwitch.prototype.fnClick=function ()
{
console.log('fnclick', this)
for(var i=0;i<this.aBtn.length;i++)
{
this.aBtn[i].className=''
this.aDiv[i].style.display='none';
}
oBtn.className='active';
this.aDiv[oBtn.index].style.display='block';
}
</script>
</head>
<body>
<div id="div1">
<input class="active" type="button" value="aaa" />
<input type="button" value="bbb" />
<input type="button" value="ccc" />
<div style="display:block;">aaa</div>
<div>dfsadf</div>
<div>erwqerwe</div>
</div>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1 input {background:white;}
#div1 input.active {background:yellow;}
#div1 div {width:200px; height:200px; background:#CCC; display:none;}
</style>
<script>
window.onload=function ()
{
new TabSwitch('div1');
};
function TabSwitch(id)
{
var _this=this;
var oDiv=document.getElementById(id);
this.aBtn=oDiv.getElementsByTagName('input');
this.aDiv=oDiv.getElementsByTagName('div');
for(var i=0;i<this.aBtn.length;i++)
{
this.aBtn[i].index=i;
this.aBtn[i].onclick=function ()
{
_this.fnClick();
};
}
};
TabSwitch.prototype.fnClick=function ()
{
//alert(this);
console.log('fnClick', this)
for(var i=0;i<this.aBtn.length;i++)
{
this.aBtn[i].className=''
this.aDiv[i].style.display='none';
}
oBtn.className='active';
this.aDiv[oBtn.index].style.display='block';
}
</script>
</head>
<body>
<div id="div1">
<input class="active" type="button" value="aaa" />
<input type="button" value="bbb" />
<input type="button" value="ccc" />
<div style="display:block;">aaa</div>
<div>dfsadf</div>
<div>erwqerwe</div>
</div>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1 input {background:white;}
#div1 input.active {background:yellow;}
#div1 div {width:200px; height:200px; background:#CCC; display:none;}
</style>
<script>
window.onload=function ()
{
new TabSwitch('div1');
};
function TabSwitch(id)
{
var _this=this;
var oDiv=document.getElementById(id);
this.aBtn=oDiv.getElementsByTagName('input');
this.aDiv=oDiv.getElementsByTagName('div');
for(var i=0;i<this.aBtn.length;i++)
{
this.aBtn[i].index=i;
this.aBtn[i].onclick=function ()
{
_this.fnClick(this);
};
}
};
TabSwitch.prototype.fnClick=function (oBtn)
{
//alert(this);
for(var i=0;i<this.aBtn.length;i++)
{
this.aBtn[i].className=''
this.aDiv[i].style.display='none';
}
oBtn.className='active';
this.aDiv[oBtn.index].style.display='block';
}
</script>
</head>
<body>
<div id="div1">
<input class="active" type="button" value="aaa" />
<input type="button" value="bbb" />
<input type="button" value="ccc" />
<div style="display:block;">aaa</div>
<div>dfsadf</div>
<div>erwqerwe</div>
</div>
</body>
</html>