JavaScript魅力总结(13-14)

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

. . . . .

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值