一個簡單的jquery左右列表內容切換應用

來源:互聯網
上載者:User

標籤:pen   jquer   sam   文字框   技術分享   select2   png   樣式   ddc   

選中左邊某個選項點擊添加,即可將選中項添加到右邊文字框中,點擊選中全部即可將全部選項移到右邊,移除按鈕功能相同。

html代碼:

 1 <div id="main"> 2     <div id="left"> 3         <div id="select1" class="sel"> 4             <span>apple</span> 5             <span>huawei</span> 6             <span>htc</span> 7             <span>samsung</span> 8         </div> 9         <button id="add" value="選中添加">選中添加</button><br>10         <button id="add_all" value="選中全部">選中全部</button>11     </div>12     <div id="right">13         <div id="select2" class="sel">14             15         </div>16         <button id="remove" value="移除添加">移除添加</button><br>17         <button id="remove_all" value="移除全部">移除全部</button>18     </div>19 </div>

css:

 1 <style type="text/css"> 2         .sel{ 3             width: 100px; 4             height: 160px; 5             border:1px solid black; 6         } 7         span{ 8             display: block; 9         }10         #left,#right{11             float: left;12             border:1px solid black;13             margin-right: 20px;14         }15         .light{16             background-color: blue;17         }18     </style>

jquery:

 1 <script type="text/javascript"> 2     $(‘span‘).click(function(){ 3         $(this).addClass("light").siblings().removeClass("light"); //當點擊span標籤時為它添加高亮樣式light,並移除除選中項外的其他同級標籤的light屬性; 4     }); 5     $("#add").click(function(){ 6         var options=$("#select1>.light");//擷取選中項; 7         options.removeClass("light").appendTo("#select2"); //將該span標籤的light屬性移除並移到右邊; 8  9     });10     $("#add_all").click(function(){11         var options=$("#select1 span");12         options.appendTo("#select2");13     });14     $("#remove").click(function(){15         var options=$(".light");16         options.removeClass("light").appendTo("#select1");17     });18     $("#remove_all").click(function(){19         var options=$("#select2 span");20         options.appendTo("#select1");21     });22 </script>

 

一個簡單的jquery左右列表內容切換應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.