DOM事件使用

练习2:使用js 练习DOM处理事件

源代码:

效果图见附件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>无标题页</title>
<style type="text/css">
ul
{
list-style:none;
}
#Select1
{
border-bottom-style:solid;
border:solid 1px black;
position:absolute;
width:200px;
height:300px;
top:10px;
left:200px;
}

#Select2
{
border-bottom-style:solid;
border:solid 1px black;
position:absolute;
top:10px;
right:300px;
width:200px;
height:300px;
}
#center
{
position:absolute;
right:500px;
left:400px;
}


.style3
{
left: 400px;
top: 15px;
height: 290px;
right: 564px;
}


.style4
{
width: 194px;
right: 300px;
top: 10px;
left: 750px;
height: 296px;
}


.style5
{
width: 194px;
left: 200px;
top: 10px;
}


.style6
{
width: 44px;
}


</style>

</head>
<body>
<!--<div id="left" class="style2">-->
<select id="Select1" class="style5" size="18" multiple="multiple">
<option selected="selected">选项1</option>
<option>选项2</option>
<option>选项3</option>
<option>选项4</option>
<option>选项5</option>
<option>选项6</option>
</select>
<!--</div>-->
<div id="center" class="style3" >
<ul>
<li><input id="Button1" type="button" value="-->" class="style6" οnclick="return Button1_onclick()" /></li>
<li><input id="Button2" type="button" value="==>" class="style6" οnclick="return Button2_onclick()" /></li>
<li><input id="Button3" type="button" value="<--" class="style6" οnclick="return Button3_onclick()" /></li>
<li><input id="Button4" type="button" value="<==" class="style6" οnclick="return Button4_onclick()" /></li>
</ul>
</div>
<!--<div id="right" class="style1"></div> -->
<select id="Select2" class="style4" size="18" multiple="multiple">
<!-- <option>选项1</option>
<option>选项2</option>
<option>选项3</option>
<option>选项4</option>
<option>选项5</option>
<option>选项6</option>
-->
</select>
<script language="javascript" type="text/javascript">
document.getElementById("Button1").onclick = Button1_onclick;
var selects = document.getElementById("Select1");
var otherSelect = document.getElementById("Select2");
var elements = selects.length;
function Button1_onclick() {
for(var i =0 ; i<elements ; i++ ){
if(selects.options[i].selected){
var option = new Option(selects.options[i].innerHTML ,i+1);
otherSelect.options[otherSelect.length] = option;
selects.removeChild(selects.options[i]);
}
}//for
}

function Button2_onclick() {
if(confirm("你确定要全部移动吗?") ){
for(var i =0 ; i< elements ; i++){
var option = new Option(selects.options[i].innerHTML ,1);
otherSelect.options[otherSelect.length] = option;
selects.removeChild(selects.options[i]);
}
return true;
}
return false;
}

function Button3_onclick() {
for(var i =0 ; i<otherSelect.length ; i++ ){
if(otherSelect.options[i].selected){
var option = new Option(otherSelect.options[i].innerHTML ,1);
selects.options[selects.length] = option;
otherSelect.removeChild(otherSelect.options[i]);
}
}//for
}
function Button4_onclick() {
var selects = document.getElementById("Select1");
var otherSelect = document.getElementById("Select2");
var elements = otherSelect.length;
if(confirm("你确定全部移动吗?")){
for(var i = 0 ; i<elements ; i++){
var option = new Option(otherSelect.options[i].innerHTML , i);
selects.options[selects.length] = option;
otherSelect.removeChild( otherSelect.options[i] );
}
}
}

</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值