一、移入移出特效
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
*{
font-family: 微软雅黑;
}
</style>
</head>
<body>
<h1>000000000000001</h1>
<h1>000000000000002</h1>
<h1>000000000000003</h1>
<h1>000000000000004</h1>
<h1>000000000000005</h1>
</body>
<script>
objs=document.getElementsByTagName('h1');
for(i=0;i<objs.length;i++){
objs[i].onmouseenter=function(){
this.style.background='#ccc';
}//鼠标移入时背景颜色为灰色
objs[i].onmouseleave=function(){
this.style.background='#fff';
}//鼠标移出时背景颜色为白色
}
//事件函数只有在事件触发时才会解析赋值
</script>
</html>
二、循环单击换色特效
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
*{
font-family: 微软雅黑;
}
</style>
</head>
<body>
<h1>000000000000001</h1>
<h1>000000000000002</h1>
<h1>000000000000003</h1>
<h1>000000000000004</h1>
<h1>000000000000005</h1>
</body>
<script>
objs=document.getElementsByTagName('h1');
for(i=0;i<objs.length;i++){
objs[i].setAttribute('num',0);
objs[i].onclick=function(){
num=parseInt(this.getAttribute('num'));
this.setAttribute('num',parseInt(num)+1);
if(num%2==0){
this.style.background='#ccc';
}else{
this.style.background='#fff';
}
this.setAttribute('num',num+1);
}
}
</script>
</html>
三、点击显示行号
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
*{
font-family: 微软雅黑;
}
</style>
</head>
<body>
<h1>000000000000001</h1>
<h1>000000000000002</h1>
<h1>000000000000003</h1>
<h1>000000000000004</h1>
<h1>000000000000005</h1>
</body>
<script>
objs=document.getElementsByTagName('h1');
for(i=0;i<=objs.length;i++){
objs[i].setAttribute('num',i+1);
objs[i].onclick=function(){
this.innerHTML=this.getAttribute('num');
}
}
</script>
</html>
四、点击标题切换内容
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
*{
font-family: 微软雅黑;
}
</style>
</head>
<body>
<h1>多啦A梦</h1>
<img src="a.jpg" />
<h1>狗</h1>
<img src="dog.png" />
<h1>篮球</h1>
<img src="bk.png" />
</body>
<script>
objs=document.getElementsByTagName('h1');
iobjs=document.getElementsByTagName('img');
for(i=0;i<objs.length;i++){
objs[i].setAttribute('line', i);
objs[i].setAttribute('num', 0);
iobjs[i].id=i;
objs[i].onclick=function(){
num=parseInt(this.getAttribute('num'));
line=this.getAttribute('line');;
nimg=document.getElementById(line);
if(num%2==0){
//隐藏h1下面的img标签
nimg.style.display='none';
}else{
//显示h1下面的img标签
nimg.style.display='block';
}
this.setAttribute('num', num+1);
}
}
</script>
</html>
可以自己选几张照片,代码里的图片我就不放上来了,太麻烦。
五、多选、反选和全不选
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
*{
font-family: 微软雅黑;
}
</style>
</head>
<body>
<form action="">
<p>爱好:</p>
<p>
<label >
<input type="checkbox" name="" id="" />打篮球
</label>
</p>
<p>
<label >
<input type="checkbox" name="" id="" />踢足球
</label>
</p>
<p>
<label >
<input type="checkbox" name="" id="" />游泳
</label>
</p>
<p>
<label >
<input type="checkbox" name="" id="" />唱歌
</label>
</p>
</form>
<p>
<button id="qx">全选</button>
<button id="qbx">全不选</button>
<button id="fx">反选</button>
</p>
</body>
<script>
qx=document.getElementById('qx');
qbx=document.getElementById('qbx');
fx=document.getElementById('fx');
objs=document.getElementsByTagName('input');
qx.onclick=function(){
for(i=0;i<objs.length;i++){
objs[i].checked=true;
}
}
qbx.onclick=function(){
for(i=0;i<objs.length;i++){
objs[i].checked=false;
}
}
fx.onclick=function(){
for(i=0;i<objs.length;i++){
objs[i].checked=objs[i].checked?false:true;
//三元运算符:若该内容选中,则不选中;若内容未选中,则选中
}
}
</script>
</html>
六、选水果
在左侧选择栏中选中的内容会被储存到右侧的候选框
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
*{
font-family: 微软雅黑;
}
select{
width:100px;
height:150px;
}
</style>
</head>
<body>
<h1>水果选择:</h1>
<form action="javascript:">
<select name="" id="s1" size='2'>
<option value="">西瓜</option>
<option value="">冬瓜</option>
<option value="">苹果</option>
<option value="">南瓜</option>
</select>
<input type="button" value=">>" id='btn'>
<select name="" id="s2" size='2'>
</select>
</form>
</body>
<script>
btn=document.getElementById('btn');
s1=document.getElementById('s1');
s2=document.getElementById('s2');
opts=s1.options;
btn.onclick=function(){
idx=s1.selectedIndex;
s2.add(opts[idx]);
}
</script>
</html>