js實際運用

來源:互聯網
上載者:User

標籤:its   get   rip   alt   pre   page   click   btn   content   

實現效果:點擊添加,左邊框中的選中項消失,添加到右邊的框中;點擊移除,右邊的不消失,但會增加到左邊的框中。

代碼:

<%@ 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 {            width: 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);        }    }};

 

js實際運用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.