Rumah > hujung hadapan web > html tutorial > JavaScript基础2下拉列表左右选择

JavaScript基础2下拉列表左右选择

WBOY
Lepaskan: 2016-09-12 17:27:13
asal
1868 orang telah melayarinya
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;"><span style="color: #ff00ff;">DOCTYPE html</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 2</span> <span style="color: #0000ff;"><span style="color: #800000;">html</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 3</span>     <span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 4</span>         <span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">charset</span><span style="color: #0000ff;">="UTF-8"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 5</span>         <span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>下拉列表左右选择<span style="color: #0000ff;"></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 6</span>         <span style="color: #0000ff;"><span style="color: #800000;">style </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text/css"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 7</span> <span style="background-color: #f5f5f5; color: #800000;">            div </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;"> 8</span> <span style="background-color: #f5f5f5; color: #ff0000;">                width</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 200px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;"> 9</span> <span style="background-color: #f5f5f5; color: #ff0000;">                float</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> left</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">10</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;">11</span> <span style="background-color: #f5f5f5; color: #800000;">            select </span><span style="background-color: #f5f5f5; color: #000000;">{</span>
<span style="color: #008080;">12</span> <span style="background-color: #f5f5f5; color: #ff0000;">                width</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 100px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">13</span> <span style="background-color: #f5f5f5; color: #ff0000;">                height</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 180px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">14</span> <span style="background-color: #f5f5f5; color: #ff0000;">                padding</span><span style="background-color: #f5f5f5; color: #000000;">:</span><span style="background-color: #f5f5f5; color: #0000ff;"> 10px</span><span style="background-color: #f5f5f5; color: #000000;">;</span>
<span style="color: #008080;">15</span>             <span style="background-color: #f5f5f5; color: #000000;">}</span>
<span style="color: #008080;">16</span>         <span style="color: #0000ff;"></span><span style="color: #800000;">style</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">17</span>     <span style="color: #0000ff;"></span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">18</span>     <span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">19</span>         <span style="color: #0000ff;"><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">20</span>             <span style="color: #0000ff;"><span style="color: #800000;">select </span><span style="color: #ff0000;">multiple</span><span style="color: #0000ff;">="multiple"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="leftSel"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="margin-left: 17px;"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">21</span>                 <span style="color: #0000ff;"><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>选项1<span style="color: #0000ff;"></span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">22</span>                 <span style="color: #0000ff;"><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>选项2<span style="color: #0000ff;"></span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">23</span>                 <span style="color: #0000ff;"><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>选项3<span style="color: #0000ff;"></span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">24</span>                 <span style="color: #0000ff;"><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>选项4<span style="color: #0000ff;"></span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">25</span>                 <span style="color: #0000ff;"><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>选项5<span style="color: #0000ff;"></span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">26</span>                 <span style="color: #0000ff;"><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>选项6<span style="color: #0000ff;"></span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">27</span>                 <span style="color: #0000ff;"><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>选项7<span style="color: #0000ff;"></span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">28</span>                 <span style="color: #0000ff;"><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>选项8<span style="color: #0000ff;"></span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">29</span>                 <span style="color: #0000ff;"><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>选项9<span style="color: #0000ff;"></span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">30</span>                 <span style="color: #0000ff;"><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>选项10<span style="color: #0000ff;"></span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">31</span>             <span style="color: #0000ff;"></span><span style="color: #800000;">select</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">32</span>             <span style="color: #0000ff;"><span style="color: #800000;">br </span><span style="color: #0000ff;">/></span>
<span style="color: #008080;">33</span>             <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="button"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="选中添加到右边 ->"</span><span style="color: #ff0000;"> onclick</span><span style="color: #0000ff;">="choiceToRight()"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">34</span>             <span style="color: #0000ff;"><span style="color: #800000;">br </span><span style="color: #0000ff;">/></span>
<span style="color: #008080;">35</span>             <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="button"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="全部添加到右边 -->"</span><span style="color: #ff0000;"> onclick</span><span style="color: #0000ff;">="allToRight()"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">36</span>         <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">37</span>         
<span style="color: #008080;">38</span>         <span style="color: #0000ff;"><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">39</span>             <span style="color: #0000ff;"><span style="color: #800000;">select </span><span style="color: #ff0000;">multiple</span><span style="color: #0000ff;">="multiple"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="rightSel"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="margin-left: 17px;"</span><span style="color: #0000ff;">></span><span style="color: #800000;">select</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">40</span>             <span style="color: #0000ff;"><span style="color: #800000;">br </span><span style="color: #0000ff;">/></span>
<span style="color: #008080;">41</span>             <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="button"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="<span style="color: #ff0000;"> onclick</span><span style="color: #0000ff;">="choiceToLeft()"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">42</span>             <span style="color: #0000ff;"><span style="color: #800000;">br </span><span style="color: #0000ff;">/></span>
<span style="color: #008080;">43</span>             <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="button"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="<span style="color: #ff0000;"> onclick</span><span style="color: #0000ff;">="allToLeft()"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">44</span>         <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">45</span>     
<span style="color: #008080;">46</span>         <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text/javascript"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">47</span>             <span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;"> 获取select</span>
<span style="color: #008080;">48</span>             <span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> leftSel </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> document.getElementById(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">leftSel</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);
</span><span style="color: #008080;">49</span>             <span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> rightSel </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> document.getElementById(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">rightSel</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);
</span><span style="color: #008080;">50</span>             <span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;"> 选中添加到右边</span>
<span style="color: #008080;">51</span>             <span style="background-color: #f5f5f5; color: #0000ff;">function</span><span style="background-color: #f5f5f5; color: #000000;"> choiceToRight() {
</span><span style="color: #008080;">52</span> <span style="background-color: #f5f5f5; color: #000000;">                toSel(leftSel, rightSel, </span><span style="background-color: #f5f5f5; color: #0000ff;">true</span><span style="background-color: #f5f5f5; color: #000000;">);
</span><span style="color: #008080;">53</span> <span style="background-color: #f5f5f5; color: #000000;">            }
</span><span style="color: #008080;">54</span>             <span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;"> 全部添加到右边</span>
<span style="color: #008080;">55</span>             <span style="background-color: #f5f5f5; color: #0000ff;">function</span><span style="background-color: #f5f5f5; color: #000000;"> allToRight() {
</span><span style="color: #008080;">56</span> <span style="background-color: #f5f5f5; color: #000000;">                toSel(leftSel, rightSel, </span><span style="background-color: #f5f5f5; color: #0000ff;">false</span><span style="background-color: #f5f5f5; color: #000000;">);
</span><span style="color: #008080;">57</span> <span style="background-color: #f5f5f5; color: #000000;">            }
</span><span style="color: #008080;">58</span>             <span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;"> 选中添加到左边</span>
<span style="color: #008080;">59</span>             <span style="background-color: #f5f5f5; color: #0000ff;">function</span><span style="background-color: #f5f5f5; color: #000000;"> choiceToLeft() {
</span><span style="color: #008080;">60</span> <span style="background-color: #f5f5f5; color: #000000;">                toSel(rightSel, leftSel, </span><span style="background-color: #f5f5f5; color: #0000ff;">true</span><span style="background-color: #f5f5f5; color: #000000;">);
</span><span style="color: #008080;">61</span> <span style="background-color: #f5f5f5; color: #000000;">            }
</span><span style="color: #008080;">62</span>             <span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;"> 全部添加到左边</span>
<span style="color: #008080;">63</span>             <span style="background-color: #f5f5f5; color: #0000ff;">function</span><span style="background-color: #f5f5f5; color: #000000;"> allToLeft() {
</span><span style="color: #008080;">64</span> <span style="background-color: #f5f5f5; color: #000000;">                toSel(rightSel, leftSel, </span><span style="background-color: #f5f5f5; color: #0000ff;">false</span><span style="background-color: #f5f5f5; color: #000000;">);
</span><span style="color: #008080;">65</span> <span style="background-color: #f5f5f5; color: #000000;">            }
</span><span style="color: #008080;">66</span>             <span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;"> 如果flag为true,就是选中添加,如果为false,就是全部添加</span>
<span style="color: #008080;">67</span>             <span style="background-color: #f5f5f5; color: #0000ff;">function</span><span style="background-color: #f5f5f5; color: #000000;"> toSel(fromSel, toSel, flag) {
</span><span style="color: #008080;">68</span>                 <span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> subSel </span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"> fromSel.getElementsByTagName(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">option</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);
</span><span style="color: #008080;">69</span>                 <span style="background-color: #f5f5f5; color: #0000ff;">if</span><span style="background-color: #f5f5f5; color: #000000;"> (flag) {
</span><span style="color: #008080;">70</span>                     <span style="background-color: #f5f5f5; color: #0000ff;">for</span><span style="background-color: #f5f5f5; color: #000000;"> (</span><span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> i </span><span style="background-color: #f5f5f5; color: #000000;">=</span> <span style="background-color: #f5f5f5; color: #000000;">0</span><span style="background-color: #f5f5f5; color: #000000;">; i </span><span style="background-color: #f5f5f5; color: #000000;"><span style="background-color: #f5f5f5; color: #000000;"> subSel.length; i</span><span style="background-color: #f5f5f5; color: #000000;">++</span><span style="background-color: #f5f5f5; color: #000000;">) {
</span><span style="color: #008080;">71</span>                         <span style="background-color: #f5f5f5; color: #0000ff;">if</span><span style="background-color: #f5f5f5; color: #000000;"> (subSel[i].selected) {
</span><span style="color: #008080;">72</span> <span style="background-color: #f5f5f5; color: #000000;">                            toSel.appendChild(subSel[i]);
</span><span style="color: #008080;">73</span>                             <span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;"> 因为subSel的length每次会-1,所以让i归零,保证每次for循环都能被执行到</span>
<span style="color: #008080;">74</span> <span style="background-color: #f5f5f5; color: #000000;">                            i</span><span style="background-color: #f5f5f5; color: #000000;">--</span><span style="background-color: #f5f5f5; color: #000000;">;
</span><span style="color: #008080;">75</span> <span style="background-color: #f5f5f5; color: #000000;">                        }
</span><span style="color: #008080;">76</span> <span style="background-color: #f5f5f5; color: #000000;">                    }
</span><span style="color: #008080;">77</span> <span style="background-color: #f5f5f5; color: #000000;">                } </span><span style="background-color: #f5f5f5; color: #0000ff;">else</span><span style="background-color: #f5f5f5; color: #000000;"> {
</span><span style="color: #008080;">78</span>                     <span style="background-color: #f5f5f5; color: #0000ff;">for</span><span style="background-color: #f5f5f5; color: #000000;"> (</span><span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> i </span><span style="background-color: #f5f5f5; color: #000000;">=</span> <span style="background-color: #f5f5f5; color: #000000;">0</span><span style="background-color: #f5f5f5; color: #000000;">; i </span><span style="background-color: #f5f5f5; color: #000000;"><span style="background-color: #f5f5f5; color: #000000;"> subSel.length; i</span><span style="background-color: #f5f5f5; color: #000000;">++</span><span style="background-color: #f5f5f5; color: #000000;">) {
</span><span style="color: #008080;">79</span> <span style="background-color: #f5f5f5; color: #000000;">                        toSel.appendChild(subSel[i]);
</span><span style="color: #008080;">80</span> <span style="background-color: #f5f5f5; color: #000000;">                        i</span><span style="background-color: #f5f5f5; color: #000000;">--</span><span style="background-color: #f5f5f5; color: #000000;">;
</span><span style="color: #008080;">81</span> <span style="background-color: #f5f5f5; color: #000000;">                    }
</span><span style="color: #008080;">82</span> <span style="background-color: #f5f5f5; color: #000000;">                }
</span><span style="color: #008080;">83</span> <span style="background-color: #f5f5f5; color: #000000;">            }
</span><span style="color: #008080;">84</span>         <span style="color: #0000ff;"></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">85</span>     <span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">86</span> <span style="color: #0000ff;"></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
Salin selepas log masuk

 

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan