zoukankan      html  css  js  c++  java
  • js实际运用

    实现效果:点击添加,左边框中的选中项消失,添加到右边的框中;点击移除,右边的不消失,但会增加到左边的框中。

    代码:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <style>
            #ListBox1, #ListBox2 {
                 200px;
                height: 200px;
                float: left;
            }
        </style>
    
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ListBox ID="ListBox1" SelectionMode="Multiple" runat="server">
                    <asp:ListItem Value="1111">苹果</asp:ListItem>
                    <asp:ListItem Value="222">橘子</asp:ListItem>
                    <asp:ListItem Value="333">香蕉</asp:ListItem>
                    <asp:ListItem Value="444">葡萄</asp:ListItem>
                    <asp:ListItem Value="55">张柯</asp:ListItem>
                </asp:ListBox>
                <div style="float: left;">
                    <input type="button" id="btn1" value="添加>>>" /><br />
                    <input type="button" id="btn2" value="<<<移除" />
                    <input type="button" id="btn_ok" value="确定" />
                </div>
                <asp:ListBox ID="ListBox2" runat="server"></asp:ListBox>
    
            </div>
        </form>
    </body>
    </html>
    
    <script src="JavaScript.js"></script>
    <script src="JavaScript2.js"></script>

    两段js码,第一个为添加,第二个为移除;移除中,创建一个新对象,将原对象的值赋给新对象,添加新对象,则原对象不会消失

    var oBtn1 = document.getElementById("btn1");
    
    oBtn1.onclick = function () {
        //取出ListBox1中的选中项
    
        var lb1 = document.getElementById("ListBox1");
    
        for (var i = 0; i < lb1.options.length; i++) {
            if (lb1.options[i].selected == true) {
                document.getElementById("ListBox2").appendChild(lb1.options[i]);
            }
    
        }
    };
    var oBtn2 = document.getElementById("btn2");
    
    oBtn2.onclick = function () {
    
        var olb2 = document.getElementById("ListBox2");
    
        for (var i = 0; i < olb2.options.length; i++) {
            if (olb2.options[i].selected == true) {
                var op = document.createElement("option");
                op.value = olb2.options[i].value;
                op.innerHTML = olb2.options[i].innerHTML;
    
                document.getElementById("ListBox1").appendChild(op);
    
            }
        }
    };
  • 相关阅读:
    android中listview分页加载数据
    android listview的HeadView左右切换图片(仿新浪,网易,百度等切换图片)
    Opencv cvCircle函数
    我是怎样成长为系统架构师的
    C++游戏编程(一开篇)
    cidaemon.exe进程cpu占用率高及关闭cidaemon.exe进程方法
    curl命令具体解释
    HDU 4334 Trouble
    美国地名大全(美国城市名称英文、中文)
    几种常见模式识别算法整理和总结
  • 原文地址:https://www.cnblogs.com/wy1992/p/6278639.html
Copyright © 2011-2022 走看看