javascript经典题目
1. 实现列表框元素的左右移动。
move.html
<script type="text/javascript">
function move(a,b)
{
var aObj = document.getElementById(a);
var bObj = document.getElementById(b);
for(var i=0;i<aObj.length;i++)
{
if(aObj[i].selected)
{
var opt = new Option(aObj[i].text,aObj[i].value);
bObj.add(opt);
aObj.remove(i);
i--;
}
}
}
</script>
<table width="40%" border="1" bgcolor="#cecece">
<tr>
<td><select id="left" size="6" multiple>
<option value="1">增加简历</option>
<option value="2">删除简历</option>
<option value="3">修改简历</option>
<option value="4">列表简历</option>
</select></td>
<td><input type="button" value=">>"
onclick="move('left','right')"><br>
<input type="button" value="<<"
onclick="move('right','left')"></td>
<td><select id="right" size="6" multiple style="width: 80px">
</select></td>
</tr>
</table>
2. 实现姓名和学号每隔一秒钟互换的效果。
change.html
<script>
//每隔一秒钟学号和姓名互换
var i = 0;
window.onload=change;
setInterval('change()',1000);
function change()
{
if(i%2)
{
document.getElementById("result").innerHTML='姓名:张三';
}else
{
document.getElementById("result").innerHTML='学号:0303003';
}
i++;
}
</script>
<div id="result"></div>
3. 动态显示当前日期时间。
datetime.html
<script>
window.onload=init;
setInterval('init()',1000);
function init()
{
var date = new Date();
var year = date.getYear();
var month = date.getMonth()+1;
if(month<10)month="0"+month;
var day = date.getDate();
if(day<10)day="0"+day;
var hour = date.getHours();
if(hour<10)hour="0"+hour;
var minute = date.getMinutes();
if(minute<10)minute="0"+minute;
var second = date.getSeconds();
if(second<10)second="0"+second;
var dateTime = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
document.getElementById("dateTime").value= dateTime;
}
</script>
<input type="text" name="dateTime" readonly>
4. 实现全选和反选。
check.html
<script>
//全选
function checkAll()
{
var chAll = document.getElementById("ckAll");
var items = document.getElementsByName("item");
for(var i = 0;i<items.length;i++)