xiaoniao

          javascript小經(jīng)驗

          獲取select選定的元素
          var   Obj   =   document.getElementById("selectID"); 
          Obj.option[Obj.selectedIndex].value
          如何用Javascript的內(nèi)置函數(shù)把以逗號分開的字符串轉(zhuǎn)化成一個數(shù)組。然后輸出數(shù)組。 
            <script> 
            str="a,b,c" 
            arr=str.split(","); 
            for(i=0;i<arr.length;i++)alert(i+":"+arr[i]) 
            </script> 
          如何用自己寫的函數(shù)把以逗號分開的字符串轉(zhuǎn)化成一個數(shù)組。然后輸出數(shù)組。 
            <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)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          <2007年7月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 华容县| 黔江区| 鞍山市| 西宁市| 邛崃市| 蛟河市| 吉木乃县| 贺兰县| 宁海县| 贺州市| 中阳县| 邓州市| 青铜峡市| 农安县| 恩平市| 遵义县| 珠海市| 西畴县| 长沙市| 谢通门县| 清河县| 葵青区| 镇雄县| 平远县| 浦城县| 台中市| 富源县| 赣榆县| 屏东市| 茂名市| 新民市| 修文县| 佳木斯市| 靖边县| 镇赉县| 罗田县| 富平县| 宝鸡市| 迭部县| 沿河| 乐东|