练习题
Posted 小学员G
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了练习题相关的知识,希望对你有一定的参考价值。
事件总结
通用:
onclick 鼠标单击
ondblclick 鼠标双击
onmouseover 鼠标放上
onmouseout 鼠标离开
onmousemove 鼠标移动
表单:
onchang 表单的值改变
onblur 失去焦点
onfocus 获得焦点
onselect 选中
1.单选按钮确定提交是否可用
<div style="width:500px; height:500px;">
<div style="margin-top:20px;">
<input type="radio" name="sure" id="s1" onclick="KeYong()" />同意
<input type="radio" name="sure" id="s2" onclick="BuKeYong()" />不同意
</div>
<div style="margin-top:30px">
<input type="submit" value="确定" id="btn" style="width:100px; height:35px;" disabled="disabled" />
</div>
</div>
<script type="text/javascript">
function KeYong()
{
//找到按钮
var a = document.getElementById("btn");
//操作按钮属性
a.removeAttribute("disabled");
}
function BuKeYong()
{
var a = document.getElementById("btn");
a.setAttribute("disabled","disabled");
}
</script>
2.倒计时按钮可用
<div style="width:500px; height:500px; margin:100px 0px 0px 100px">
<input id="btn" type="submit" value="确定" style="width:100px; height:35px" disabled="disabled" />
<span id="daojishi">10</span>
<div style="margin-top:20px">
<span id="h"></span>
<span id="m"></span>
<span id="s"></span>
</div>
</div>
<script type="text/javascript">
window.setTimeout("YanChi()",1000);
function YanChi()
{
var span = document.getElementById("daojishi");
//改变span里面的值
span.innerText = span.innerText-1;
//判断是否减到了0
if(span.innerText == 0)
{
document.getElementById("btn").removeAttribute("disabled");
return;
}
window.setTimeout("YanChi()",1000);
}
window.setInterval("Bian()",1000);
function Bian()
{
var sj = new Date();
document.getElementById("h").innerText = sj.getHours();
document.getElementById("m").innerText = sj.getMinutes();
document.getElementById("s").innerText = sj.getSeconds();
}
</script>
3.向列表内添加数据
<select id="sel" style="width:200px;" size="10">
<option>11</option>
<option>22</option>
<option>33</option>
</select>
<input type="text" id="nr" />
<input type="button" value="添加" id="btn1" onclick="Add()" />
<script type="text/javascript">
function Add()
{
//取出用户输入的值
/*var v = document.getElementById("nr").value;
//向列表里面添加
var list = document.getElementById("sel");
list.innerhtml += "<option>"+v+"</option>";*/
alert(document.getElementById("sel").value);
}
4.两个列表之间移动数据
<div style="width:200px; height:300px; float:left">
<select id="list1" size="10" style="width:200px; height:300px">
<option>山东</option>
<option>北京</option>
<option>河北</option>
<option>黑龙江</option>
<option>河南</option>
</select>
</div>
<div style="width:80px; height:300px; float:left">
<input type="button" value="单移" id="btn1" style="width:70px; height:30px" onclick="Dan()"/>
<input type="button" value="全移" id="btn2" style="width:70px; height:30px" onclick="Duo()"/>
</div>
<div style="width:200px; height:300px; float:left">
<select id="list2" size="10" style="width:200px; height:300px">
</select>
</div>
function Dan()
{
//把列表1选中值取出
var list1 = document.getElementById("list1");
var v = list1.value;
//造一个option项
var s = "<option class=‘o2‘>"+v+"</option>";
//判断list2里面是否有该项
var attr = document.getElementsByClassName("o2");
var cz = true;
for(var i=0;i<attr.length;i++)
{
//alert(attr[i].innerHTML);
if(attr[i].innerHTML==v)
{
cz = false;
break;
}
}
if(cz)
{
//将option项扔到list2
var list2 = document.getElementById("list2");
list2.innerHTML +=s;
}
}
function Duo()
{
document.getElementById("list2").innerHTML = document.getElementById("list1").innerHTML;
}
5.日期时间选择
<select id="year">
</select>
年
<select id="month" onchange="FillDay()">
</select>
月
<select id="day">
</select>
日
FillYear();
FillMonth();
FillDay();
function FillYear()
{
var sj = new Date();
var nian = sj.getFullYear();
var s = "";
for(var i=nian-5;i<nian+6;i++)
{
if(i==nian)
{
s +="<option selected=‘selected‘>"+i+"</option>";
}
else
{
s +="<option>"+i+"</option>";
}
}
document.getElementById("year").innerHTML = s;
function FillMonth()
{
var sj = new Date();
var yue = sj.getMonth()+1;
var s = "";
for(var i=1;i<13;i++)
{
if(i==yue)
{
s +="<option selected=‘selected‘>"+i+"</option>";
}
else
{
s +="<option>"+i+"</option>";
}
}
document.getElementById("month").innerHTML=s;
}
function FillDay()
{
var sj = new Date();
var tian = sj.getDate();
//取月份求天数
var yue = document.getElementById("month").value;
var n = 31;
if(yue==4 || yue==6 ||yue==9 ||yue==11)
{
n = 30;
}
else if(yue==2)
{
n=28;
}
//用循环添加
var s = "";
for(var i=1;i<n+1;i++)
{
if(i==tian)
{
s +="<option selected=‘selected‘>"+i+"</option>";
}
else
{
s +="<option>"+i+"</option>";
}
}
document.getElementById("day").innerHTML = s;
}
6..子菜单下拉
<style type="text/css">
*{ margin:0px auto; padding:0px}
#menu{ width:700px; height:40px; border:1px solid #999; margin-top:30px}
.list{ width:100px; height:40px; text-align:center; line-height:40px; vertical-align:middle; font-size:16px; font-family:微软雅黑; float:left}
.list:hover{ cursor:pointer; background-color:#63C; color:white}
.ziwai{width:0px; height:0px;position:relative; float:left; top:40px; left:-100px}
.zi{ width:100px; height:100px; background-color:#6C3; display:none }
</style>
<div id="menu">
<div class=‘list‘ onmouseover="Show(‘z1‘)" onmouseout="YinCang()">首页</div>
<div class="ziwai" >
<div class="zi" id="z1"></div>
</div>
<div class=‘list‘ onmouseover="Show(‘z2‘)" onmouseout="YinCang()">产品介绍</div>
<div class="ziwai" >
<div class="zi" id="z2"></div>
</div>
<div class=‘list‘ onmouseover="Show(‘z3‘)" onmouseout="YinCang()">公司简介</div>
<div class="ziwai" >
<div class="zi" id="z3"></div>
</div>
<div class=‘list‘ onmouseover="Show(‘z4‘)" onmouseout="YinCang()">联系我们</div>
<div class="ziwai" >
<div class="zi" id="z4"></div>
</div>
<div class=‘list‘ onmouseover="Show(‘z5‘)" onmouseout="YinCang()">新闻动态</div>
<div class="ziwai" >
<div class="zi" id="z5"></div>
</div>
</div>
<script type="text/javascript">
function Show(id)
{
var attr = document.getElementsByClassName("zi");
for(var i=0; i<attr.length;i++)
{
attr[i].style.display = "none";
}
document.getElementById(id).style.display = "block";
}
function YinCang()
{
var attr = document.getElementsByClassName("zi");
for(var i=0; i<attr.length;i++)
{
attr[i].style.display = "none";
}
}
</script>
7.DIV做下拉列表
<style type="text/css">
*{ margin:0px auto; padding:0px}
#xiala{ width:180px; height:33px; border:1px solid #999;text-align:center; line-height:33px; vertical-align:middle; }
#xiala:hover{ cursor:pointer}
#zi{width:180px; height:150px; border:1px solid #63C; border-top:0px; display:none}
.list{width:180px; height:33px; text-align:center; line-height:33px; vertical-align:middle; border-bottom:1px solid #63C; background-color:#CCC}
.list:hover{ cursor:pointer; background-color:#63C; color:white}
</style>
<div style="width:700px; height:500px; margin-top:30px">
<div id="xiala" onclick="Show()"></div>
<div id="zi">
<div class="list" onclick="Xuan(this)">山东</div>
<div class="list" onclick="Xuan(this)">淄博</div>
<div class="list" onclick="Xuan(this)">张店</div>
</div>
</div>
<script type="text/javascript">
function Show()
{
document.getElementById("zi").style.display="block";
}
function Xuan(ys)
{
var v = ys.innerText;
document.getElementById("xiala").innerText = v;
document.getElementById("zi").style.display="none";
}
</script>
8.图片轮播
<div style="width:1000px; height:250px; margin-top:30px">
<img src="img/11.jpg" width="1000" height="250" />
<img src="img/22.png" width="1000" height="250" style="display:none" />
<img src="img/33.png" width="1000" height="250" style="display:none" />
<img src="img/44.png" width="1000" height="250" style="display:none" />
</div>
<script type="text/javascript">
window.setInterval("Huan()",2000);
//找到图片的最大索引
var n = document.getElementsByTagName("img").length-1;
//存当前图片的索引
var d =0;
//换图
function Huan()
{
//找到所有图片
var attr = document.getElementsByTagName("img");
//当前索引加1
d++;
//判断索引是否超出范围
if(d>n)
{
d = 0;
}
//换图
//让所有隐藏
for(var i=0;i<=n;i++)
{
attr[i].style.display = "none";
}
//让该索引的显示
attr[d].style.display = "block";
}
</script>
9.选项卡效果
<style type="text/css">
*{ margin:0px auto; padding:0px}
#menu{ width:240px; height:30px;}
.list{ width:60px; height:30px; float:left; text-align:center; line-height:30px; vertical-align:middle;}
.list:hover{ cursor: pointer}
.nr{ width:240px; height:200px; text-align:center; line-height:200px; vertical-align:middle}
</style>
<div style="width:700px; height:500px; margin-top:30px">
<div id="menu">
<div class="list" style="background-color:#0F0" onclick="Show(‘d1‘)">娱乐</div>
<div class="list" style="background-color:#369" onclick="Show(‘d2‘)">社会</div>
<div class="list" style="background-color:#F60" onclick="Show(‘d3‘)">体育</div>
<div class="list" style="background-color:#CC3" onclick="Show(‘d4‘)">军事</div>
</div>
<div id="d1" class="nr" style="background-color:#3C0">娱乐新闻</div>
<div id="d2" class="nr" style="background-color:#399; display:none">社会新闻</div>
<div id="d3" class="nr" style="background-color:#F30; display:none">体育新闻</div>
<div id="d4" class="nr" style="background-color:#CF3; display:none">军事新闻</div>
</div>
<script type="text/javascript">
function Show(id)
{
//隐藏所有
var attr = document.getElementsByClassName("nr");
for(var i=0;i<attr.length;i++)
{
attr[i].style.display = "none";
}
//显示当前的
document.getElementById(id).style.display = "block";
}
</script>
10.滑动效果
<style type="text/css">
*{ margin:0px auto; padding:0px}
#left{ height:600px; background-color:#63C; float:left}
#right{ height:600px; background-color:#F33; float:left}
#btn{ width:30px; height:30px; background-color:#FFF; position:relative; top:285px; color:#60F; font-weight:bold; text-align:center; line-height:30px; vertical-align:middle; float:left}
#btn:hover{ cursor:pointer}
</style>
<div id="left" style="width:200px;">
<div id="btn" onclick="Bian()" style="left:185px;">-></div>
</div>
<div id="right" style="width:800px;"></div>
<script type="text/javascript">
function Bian()
{
Dong();
}
//改变大小和位置
function Dong()
{
var d1 = document.getElementById("left");
var d2 = document.getElementById("right");
var btn = document.getElementById("btn");
//左侧DIV变宽
var yskd1 = d1.style.width;
var w1 = yskd1.substr(0,yskd1.length-2);
var w1 = parseInt(w1)+2;
d1.style.width = w1+"px";
//右侧DIV变窄
var yskd2 = d2.style.width;
var w2 = yskd2.substr(0,yskd2.length-2);
var w2 = parseInt(w2)-2;
d2.style.width = w2+"px";
//将按钮移动
var ysjl = btn.style.left;
var w3 = ysjl.substr(0,ysjl.length-2);
var w3 = parseInt(w3)+2;
btn.style.left = w3+"px";
if(w2>200)
{
//自己调自己
window.setTimeout("Dong()",1);
}
}
</script>
11.进度条的制作
<style type="text/css">
*{ margin:0px auto; padding:0px}
#wai{ width:200px; height:10px; border:1px solid #60F;}
#nei{ width:0px; height:10px; background-color:#F33; float:left}
</style>
<div id="wai">
<div id="nei"> </div>
</div>
<script type="text/javascript">
function Start()
{
Bian();
}
var bfb = 0;
function Bian()
{
//将百分比变化
bfb= bfb+1;
//改变宽度
document.getElementById("nei").style.width = bfb+"%";
//判断
if(bfb<100)
{
window.setTimeout("Bian()",50);
}
}
</script>
以上是关于练习题的主要内容,如果未能解决你的问题,请参考以下文章