zoukankan      html  css  js  c++  java
  • jquery中each遍历各种标签方法

    这写天用到的遍历jquery each方法比较频繁 刚好有时间,就在这里记录一下

    jquery用的是bootstrap的线上文件 不需要导入

    <!DOCTYPE html>
    <html lang="zh-cn">
     <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://cdn.bootcss.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
     </head>
     <body >
     
     <div id="DIV1">
     <input type="text" value="aaaa" name="input" class="class1" id="id1"/>
     <input type="text" value="bbbb" name="input" class="class1" id="id1"/>
     <input type="text" value="cccc" name="input" class="class1" id="id1"/>
     <input type="text" value="dddd" name="input" class="class1" id="id1"/>
     <input type="text" value="eeee" name="input" class="class1" id="id1"/><br/>
     <input type="button" value="遍历input值" onclick="eachInput();"/>
     </div>
    <script>
    function eachInput(){

      /*遍历方法指定的属性不一定多种方式的  甚至可以自己定义标签没有的属性
      (如 给上面的input 加一个 test="first"    $("#DIV1 input[test=first]").each(function(){  })  )  

       也是可以遍历的只要一直就可以 */
        // $("#DIV1 input[class=class1]").each(function(){
        //$("#DIV1 input[id=id1]").each(function(){
        $("#DIV1 input[name=input]").each(function(){
            alert($(this).val());
        });
    }
    </script>     


     <div id="DIV2">
     <select name="selectname" id="selectid" class="form-control">
         <option value="1">1</option>
         <option value="2">2</option>
         <option value="3">3</option>
         <option value="4">4</option>
         <option value="5">5</option>
     </select>
     <br/>
     <input type="button" value="遍历select值" onclick="eachSelect();"/>
     </div>
    <script>    
    function eachSelect(){
        //遍历指定选中的select
        $("#DIV2 select[class=form-control]").each(function(){
            alert($(this).val());
        });
        //遍历指定选中的select的所有option值
        $("#DIV2 select[name=selectname] option").each(function(){
            alert($(this).val());
        });
        
    }
    </script> 


    <div id="DIV3">
                <ul>
                    <li>选项一</li>
                    <li>选项二</li>
                    <li>选项三</li>
                    <li>选项四</li>
                </ul>
      <input type="button" value="遍历li值" onclick="eachLi();"/>
    </div>

    <script>
    function eachLi(){
        //$(this).removeClass("selected");
        $("#DIV3 li").each(function(){
            alert($(this).html());
        });
    }
    </script>


    <div id="DIV4">
     <table id="tb" width="200" border="1">
       <tr>
         <td>1</td>
         <td>2</td>
         <td>3</td>
       </tr>
       <tr>
         <td>yi</td>
         <td>er</td>
         <td>san</td>
       </tr>
       <tr>
         <td>一</td>
         <td>二</td>
         <td>三</td>
       </tr>
     </table>
      <input type="button" value="遍历Tr值" onclick="eachTr();"/>
     </div>
     <br/>

    <script>
    function eachTr(){
        //$(this).removeClass("selected");
        //index 表示遍历的下标      tr遍历对象的别名
        $("#DIV4 tr").each(function(index,tr){
            alert($(tr).html()+" index="+index);
        });
        //这样取出地表格第三行的第一列 or 第二列的文本值
        alert($("#DIV4 table tr:eq(2)").find("td:eq(0)").text());
        alert($("#DIV4 table tr:eq(2)").find("td:eq(1)").text());
    }
    </script>


    <div id="DIV5">
      爱好:
         <input type="checkbox" value="看电视"  name="hobby" /> 看电视
         <input type="checkbox" value="游泳"   name="hobby" /> 游泳
         <input type="checkbox" value="游戏"   name="hobby" /> 游戏
         <input type="checkbox" value="把妹"   name="hobby" /> 把妹
      <input type="button" value="遍历checkbox值" onclick="eachCheckbox();"/>
     </div>

    <script>
    //遍历的另外一种方式
    function eachCheckbox(){
        $.each($('input:checkbox'),function(i,val){
              alert(val.value+"===所有的==="+val.name);
        });
        //遍历选中的复选框
        $("#DIV5 input[name=hobby]:checked").each(function(){
            alert("选中的==="+$(this).val());    
        });
    }
    </script>   

     
     <br/>
     <div>
     <p> var arr= [ "aaa", "bbb", "ccc" ];      </p>
     <input type="button" value="遍历Arr值" onclick="eachArr()"/>
     </div>
     <script>
    //遍历数组
    function eachArr(){
         var arr= [ "aaa", "bbb", "ccc" ];      
        $.each(arr,function(i,val){
            alert(i+"::"+val);
        });
    }
    </script>


     
     <div>
     <p>var obj = { one:1, two:2, three:3};     </p>
     <input type="button" value="遍历JSON值" onclick="eachJson()"/>
     </div>
     <script>
    //遍历json格式(常用于回调解析json格式数据)
    function eachJson(){
        var obj = { one:1, two:2, three:3};      
        $.each(obj,function(key,val){
            alert(key+":::"+val);   
        });
    }
    </script>
     <br/>
     


     <div id="DIV6">
         <form action="">
             <input type="text" value="表单一"/>
             <input type="button" value="表单一" onclick="eachForm(0)"/>
         </form>
         <br/>
         <form action="">
             <input type="text" value="表单二"/>
             <input type="button" value="表单二" onclick="eachForm(1)"/>
         </form>
         <br/>
         <form action="">
             <input type="text" value="表单三"/>
             <input type="button" value="表单三" onclick="eachForm(2)" />
         </form>
         <br/>
     </div>
     
    <script>
    //指定表单提交
    function eachForm(i){
        $('form').eq(i).attr('action','/form?num='+i);
        $('form').eq(i).submit();
    }
    </script>
     <br/>
     


     
     <input type="button"  value="隐藏" onclick="setProperty()"/>
     <input type="button"  value="显示" onclick="showDiv()"/>
    </body>
    <script>
    //最经常用到的jquery设置 标签的属性值 常用到的方法
    function setProperty(){
        $('#DIV3').attr("readonly",true);//将input元素设置为readonly
             $('#DIV2').attr("disabled","disabled");//将input元素设置为disabled
          $("#DIV1").css("display","none");//设置标签隐藏
        $("#DIV1 input").addClass("form-control");//设置标签的样式
        $(".form-control").removeClass("form-control").addClass("input");//移除替换样式
        $('#DIV2').removeAttr("disabled");//去除input元素的disabled属性
        $('#DIV3').removeAttr("readonly");//去除input元素的readonly属性
    }
    function showDiv(){
        $("#DIV1").hide();//隐藏
          $("#DIV1").show();//显示
    }
    </script>
    </html>

    <input type="checkbox" onclick="if(this.checked){$('input[name=checkbox]').each(function(){this.checked=true});}else{$('input[name=checkbox]').each(function(){this.checked=false});}"/>全选

  • 相关阅读:
    【python3的进阶之路一】正则表达式
    基础编程练习50道
    【python3的学习之路十四】IO编程
    【python3的学习之路十三】错误和调试
    【python3的学习之路十二】面向对象高级编程
    【python3的学习之路十一】面向对象编程
    jQuery之防止冒泡事件,冒泡事件就是点击子节点,会向上触发父节点,祖先节点的点击事件。
    手机移动端WEB资源整合
    js 验证表单 js提交验证类
    js单条新闻向上滚动
  • 原文地址:https://www.cnblogs.com/laotan/p/3958011.html
Copyright © 2011-2022 走看看