JavaScript魅力总结(1-5)

初探JavaScript魅力1

  1. 获取CSS样式的名称:
    • className
    • classList
<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
            <title>
                无标题文档
            </title>
            <style>
                #div1 {width:100px; height:100px; border:1px solid black;}
				.box {background:red;}
            </style>
            <script>
                function toRed()
				{
					var oDiv=document.getElementById('div1');
	
					oDiv.className='box';
				}
            </script>
        </meta>
    </head>
    <body>
        <input onclick="toRed()" type="button" value="变红"/>
        <div id="div1">
        </div>
    </body>
</html>
  1. 任何标签都可以加ID,包括link
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<link id="l1" rel="stylesheet" type="text/css" href="css1.css" />
<script>
function skin1()
{
	var oL=document.getElementById('l1');
	
	oL.href='css1.css';
}

function skin2()
{
	var oL=document.getElementById('l1');
	
	oL.href='css2.css';
}
</script>
</head>

<body>
<input type="button" value="皮肤1" onclick="skin1()" />
<input type="button" value="皮肤2" onclick="skin2()" />
</body>
</html>


初探JavaScript魅力2

  1. 对象中能用点的就一定能用中括号的形式,反过来则不一定。
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1 {width:200px; height:200px; background:red;}
</style>
<script>
function setStyle(name, value)
{
	var oDiv=document.getElementById('div1');
	
	oDiv.style[name]=value;
}
</script>
</head>

<body>
<input type="button" value="变宽" onclick="setStyle('width', '400px')" />
<input type="button" value="变高" onclick="setStyle('height', '400px')" />
<input type="button" value="变绿" onclick="setStyle('background', 'green')" />
<div id="div1">
</div>
</body>
</html>
  1. 选项卡实现思路:利用对象可以扩展无限属性,给对象添加index索引
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1 .active {background:yellow;}
#div1 div {width:200px; height:200px; background:#CCC; border:1px solid #999; 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 i=0;i<aBtn.length;i++)
			{
				aBtn[i].className='';
				aDiv[i].style.display='none';
			}
			// console.log(this);
			// console.log(aDiv[this.index])
			this.className='active';
			//alert(this.index);
			aDiv[this.index].style.display='block';
			// this.style.display = 'block';
		};
	}
};
</script>
</head>

<body>
<div id="div1">
	<input class="active" type="button" value="教育" />
	<input type="button" value="培训" />
	<input type="button" value="招生" />
	<input type="button" value="出国" />
    <div style="display:block;">1111</div>
    <div>2222</div>
    <div>333</div>
    <div>4444</div>
</div>
</body>
</html>

或者在标签上添加index,然后通过getAttribute()来获取index属性

<div id="div1">
	<input class="active" type="button" value="教育" index="0" />
	<input type="button" value="培训" index="1" />
	<input type="button" value="招生" index="2" />
	<input type="button" value="出国" index="3" />
    <div style="display:block;">1111</div>
    <div>2222</div>
    <div>333</div>
    <div>4444</div>
</div>

定时器的使用

  1. absolute 可以相对于前面的absolute、relative、fixed相对定位
<!DOCTYPE html>
<html>
<head>
	<title></title>
	<style type="text/css">
		#fixed {
			width: 300px;
			height: 400px;
			background: red;
			position: fixed;
			left: 20px;
			top: 100px;
		}
		#absolute {
			width: 200px;
			height: 200px;
			background: yellow;
			position: absolute;
			left: 10px;
			top: 20px;		
		}
	</style>
</head>
<body>
	<div id="fixed">
		<div id="absolute"></div>
	</div>

</body>
</html>
  1. 延迟弹出框注意事项:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
div {float:left; margin:10px;}
#div1 {width:50px; height:50px; background:red;}
#div2 {width:250px; height:180px; background:#CCC; display:none;}
</style>
<script>
window.onload=function ()
{
	var oDiv1=document.getElementById('div1');
	var oDiv2=document.getElementById('div2');
	var timer=null;
	
	oDiv1.onmouseover=function ()
	{
		//先将定时器清除
		clearTimeout(timer);
		oDiv2.style.display='block';
	};
	oDiv1.onmouseout=function ()
	{
		timer=setTimeout(function (){
			oDiv2.style.display='none';
		}, 500);
	};
	
	//在还没有将异步函数放入队列之前将定时器清除
	// clearTimeout不会在内存中销毁变量
	oDiv2.onmouseover=function ()
	{
		clearTimeout(timer);
	};
	oDiv2.onmouseout=function ()
	{
		timer=setTimeout(function (){
			oDiv2.style.display='none';
		}, 500);
	};
};
</script>
</head>

<body>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>
  1. 数码时钟
    1. 在使用setInterval()函数的时候为了避免第一次加载仍为原来的转态,那么需要先执行一下setInterval()里的那个函数。
    2. ‘abc’.charAt() //取字符串中的字符
    3. ‘bad’[0] //取字符串中的字符
  2. 无缝滚动
    1. offsetWidth获取元素对象的border+padding+width
    2. oUl.innerHTML=oUl.innerHTML+oUl.innerHTML; 复制一份
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
* {margin:0; padding:0;}
#div1 {width:712px; height:108px; margin:100px auto; position:relative; background:red; overflow:hidden;}
#div1 ul {position:absolute; left:0; top:0;}
#div1 ul li {float:left; width:178px; height:108px; list-style:none;}
</style>
<script>
window.onload=function ()
{
	var oDiv=document.getElementById('div1');
	var oUl=oDiv.getElementsByTagName('ul')[0];
	var aLi=oUl.getElementsByTagName('li');
	
	var speed=-2;
	
	//oUl.innerHTML=oUl.innerHTML+oUl.innerHTML;
	oUl.innerHTML+=oUl.innerHTML;
	oUl.style.width=aLi[0].offsetWidth*aLi.length+'px';
	
	function move(){
		//注意这里是相对于父元素的绝对定位
		if(oUl.offsetLeft<-oUl.offsetWidth/2)
		{
			oUl.style.left='0';
		}
		if(oUl.offsetLeft>0)
		{
			oUl.style.left=-oUl.offsetWidth/2+'px';
		}
		oUl.style.left=oUl.offsetLeft+speed+'px';
	}
	var timer=setInterval(move, 30);
	
	oDiv.onmouseover=function ()
	{
		clearInterval(timer);
	};
	
	oDiv.onmouseout=function ()
	{
		timer=setInterval(move, 30);
	};
	
	document.getElementsByTagName('a')[0].onclick=function ()
	{
		speed=-2;
	};
	document.getElementsByTagName('a')[1].onclick=function ()
	{
		speed=2;
	};
};
</script>
</head>

<body>
<a href="javascript:;">向左走</a>
<a href="javascript:;">向右走</a>
<div id="div1">
    <ul>
    	<li><img src="img2/1.jpg" /></li>
    	<li><img src="img2/2.jpg" /></li>
    	<li><img src="img2/3.jpg" /></li>
    	<li><img src="img2/4.jpg" /></li>
    </ul>
</div>
</body>
</html>

  1. 基础运动
<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
            <title>
                无标题文档
            </title>
            <style>
                #div1 {width:200px; height:200px; background:red; position:absolute; left:0; top:50px;}
            </style>
            <script>
                setInterval(function (){
					var oDiv=document.getElementById('div1');
					oDiv.style.left=oDiv.offsetLeft+10+'px';
				}, 30);
            </script>
        </meta>
    </head>
    <body>
        <div id="div1">
        </div>
    </body>
</html>

4 深入javascript

  1. 通过arguments对象可以实现模拟JavaScript里函数重载的功能
  2. 取非行间样式(兼容性写法),obj.currentStyle是IE的写法,getComputedStyle()是Chrome和Firefox的写法。
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1 {width:200px; height:200px; background:red;}
</style>
<script>
function getStyle(obj, name)
{
	if(obj.currentStyle)
	{
		return obj.currentStyle[name];
	}
	else
	{
		return getComputedStyle(obj, false)[name];
	}
}

window.onload=function ()
{
	var oDiv=document.getElementById('div1');
		
	alert(getStyle(oDiv, 'backgroundColor'));
};
</script>
</head>

<body>
<div id="div1">
</div>
</body>
</html>
  1. 不管是往数组前面还是后面都可以同时添加多个,例如:arr.push(2,4,5); arr.unshift(5,6,7);
    但是,从数组前面shift还是后面pop都只能一次一个,不管写不写参数。
  2. splice函数,这个函数返回删除的值
<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
            <title>
                无标题文档
            </title>
            <script>
                var arr=[1,2,3,4,5,6];

				//删除:splice(起点, 长度)
				// arr.splice(2, 3);

				//插入:splice(起点, 长度, 元素...);
				// arr.splice(2, 0, 'a', 'b', 'c');

				arr.splice(2, 2, 'a', 'b', 'd');

				alert(arr);
            </script>
        </meta>
    </head>
    <body>
    </body>
</html>

5. Ajax基础

function ajax(url, fnSucc, fnFaild)
{
	//1.创建Ajax对象
	if(window.XMLHttpRequest)
	{
		var oAjax=new XMLHttpRequest();
	}
	else
	{
		var oAjax=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	//2.连接服务器(打开和服务器的连接)
	oAjax.open('GET', url, true);
	
	
	//3.发送
	oAjax.send();
	
	//4.接收
	oAjax.onreadystatechange=function ()
	{
		if(oAjax.readyState==4)
		{
			if(oAjax.status==200)
			{
				//alert('成功了:'+oAjax.responseText);
				fnSucc(oAjax.responseText);
			}
			else
			{
				//alert('失败了');
				if(fnFaild)
				{
					fnFaild();
				}
			}
		}
	};
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

. . . . .

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

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

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

打赏作者

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

抵扣说明:

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

余额充值