xiaoniao

          javascript小經驗

          獲取select選定的元素
          var   Obj   =   document.getElementById("selectID"); 
          Obj.option[Obj.selectedIndex].value
          如何用Javascript的內置函數把以逗號分開的字符串轉化成一個數組。然后輸出數組。 
            <script> 
            str="a,b,c" 
            arr=str.split(","); 
            for(i=0;i<arr.length;i++)alert(i+":"+arr[i]) 
            </script> 
          如何用自己寫的函數把以逗號分開的字符串轉化成一個數組。然后輸出數組。 
            <script> 
            str="o,m,n" 
            arr=split2(str); 
            for(i=0;i<arr.length;i++)alert(i+":"+arr[i]) 
            function   split2(tmp){ 
            return   tmp.match(/([^,]+)/g) 
            } 
            </script> 

          Example:
          <html>
          <head>
              <title></title>

              <script language="javascript">       
                  function Add(objA,objB)
                  {
                      var tem=new Array();
                      with(objA)
                      for(i=length-1;i>=0;i--)
                          if(options[i].selected){tem[tem.length]=new Option(options[i].text,options[i].value);}
                     
                      if(objA.selectedIndex>-1)
                      {
                          for(i=0;i<objB.length;i++) tem[tem.length]=objB.options[i];
                          with(objB)
                          {
                              length=0;
                              tem.sort(sortArr);
                              for(i=0;i<tem.length;i++) options[length]=new Option(tem[i].text,tem[i].value)
                          }
                      }   
                  }
                 
                  function sortArr(a,b)
                  {
                      if(a.text>b.text)return 1;
                      if(a.text<b.text)return -1;
                      return 0;
                  }
                  //   
                 
                  //
                  function up(obj)
                  {       
                      var objO = new Option(obj.options[obj.selectedIndex].text,obj.options[obj.selectedIndex].value);   
                     
                      var selectedIndex = obj.selectedIndex;
                      if(selectedIndex>0)
                      {               
                          obj.options[selectedIndex].text = obj.options[selectedIndex-1].text;
                          obj.options[selectedIndex].value = obj.options[selectedIndex-1].value;
                         
                          obj.options[selectedIndex-1].text = objO.text;
                          obj.options[selectedIndex-1].value = objO.value;
                         
                          obj.selectedIndex = selectedIndex-1;       
                      }
                  }
                  //
                 
                  //
                  function down(obj)
                  {       
                      var objO = new Option(obj.options[obj.selectedIndex].text,obj.options[obj.selectedIndex].value);   
                     
                      var selectedIndex = obj.selectedIndex;
                      if(selectedIndex<obj.options.length-1)
                      {               
                          obj.options[selectedIndex].text = obj.options[selectedIndex+1].text;
                          obj.options[selectedIndex].value = obj.options[selectedIndex+1].value;
                         
                          obj.options[selectedIndex+1].text = objO.text;
                          obj.options[selectedIndex+1].value = objO.value;
                         
                          obj.selectedIndex = selectedIndex+1;
                      }
                  }
                  //
                 
                  // 雙擊時添加到b
                  function dba(objA,objB)
                  {           
                      var objO=new Option(objA.options[objA.selectedIndex].text,objA.options[objA.selectedIndex].value)
                      objB.add(objO);
                  }
                  //
                  // 雙擊時刪除b
                  function dbb(obj)
                  {
                      obj.removeChild(obj.options[obj.selectedIndex]);
                  }
                  //
                 
                  // 刪除b中選中的項
                  function del(obj)
                  {       
                      for(var i=obj.options.length-1;i>=0;i--)
                      {
                          if(obj.options[i].selected)
                          {
                              obj.removeChild(obj.options[i]);
                          }
                      }           
                  }
                  //
                 
                  //
                  function getvalue(obj)
                  {
                      var str="";
                     
                      for(var i=0;i<obj.options.length;i++)
                      {
                          if(str.length>0)
                              str = str + "," + obj.options[i].value;
                          else
                              str = obj.options[i].value;
                      }
                     
                      document.getElementById("selectValue").innerText = str;
                  }
                  //
              </script>

          </head>
          <body bgcolor="#CCCCCC">
              <table cellspacing="0" cellpadding="0" width="448" border="0">
                  <tr>
                      <td width="45%" align="center">
                          <select style="width: 155px" multiple size="15" name="a" ondblclick="dba(this,document.getElementById('b'))">
                              <option value="av1" title="av1">at1</option>
                              <option value="av2" title="av2" style="background-color:#999999">at2</option>
                              <option value="av3" title="av3">at3</option>
                              <option value="av4" title="av4">at4</option>
                              <option value="av5" title="av5">at5</option>
                              <option value="av6" title="av6">at6</option>
                              <option value="av7" title="av7">at7</option>
                          </select>
                      </td>
                      <td>
                          <input onClick="Add(document.getElementById('a'),document.getElementById('b'))" type="button"
                              value="&gt;&gt;" style="width: 40px">
                          <br>
                          <input onClick="del(document.getElementById('b'))" type="button" value="Del" style="width: 40px">
                          <br>
                          <input onClick="up(document.getElementById('b'))" type="button" value="b↑" style="width: 40px">
                          <br>
                          <input onClick="down(document.getElementById('b'))" type="button" value="b↓" style="width: 40px">
                      </td>
                      <td width="45%" align="center">
                          <select style="width: 155px" multiple size="15" name="b" ondblclick="dbb(this)">
                              <option value="bv1">bt1</option>
                              <option value="bv2">bt2</option>
                              <option value="bv3">bt3</option>
                              <option value="bv4">bt4</option>
                              <option value="bv5">bt5</option>
                              <option value="bv6">bt6</option>
                              <option value="bv7">bt7</option>
                              <option value="bv8">bt8</option>
                          </select>
                      </td>
                  </tr>
                  <tr>
                      <td colspan="3" align="center">
                          <input type="button" name="button1" value="獲得右邊select值" onClick="getvalue(document.getElementById('b'))"></td>
                  </tr>
                  <tr>
                      <td colspan="3" align="center">
                          <div id="selectValue">
                          </div>
                      </td>
                  </tr>
              </table>
          </body>
          </html>

          posted on 2007-07-05 08:32 小鳥 閱讀(453) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2007年7月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 岳阳县| 长海县| 石城县| 吴旗县| 富顺县| 瑞安市| 双流县| 子长县| 稷山县| 永兴县| 湄潭县| 策勒县| 琼中| 平利县| 合山市| 弋阳县| 湘阴县| 睢宁县| 墨江| 杂多县| 镇坪县| 兴义市| 江都市| 乌兰县| 台前县| 保定市| 阿克陶县| 南宁市| 二连浩特市| 霍州市| 旺苍县| 灵寿县| 蓬安县| 黔南| 潞城市| 来凤县| 扎囊县| 屯门区| 保定市| 天镇县| 宁武县|