zoukankan      html  css  js  c++  java
  • jquery 的 for 循环

    jquery 的 for 循环:

    1、 var userList = [11,22,33,44];

      $.each(userList,function(i,item){

        console.log(i, item);

      });

      结果输出:

          0 11

        1 22

      2 33

      3 44

    用法:  $.each()  第一个参数是循环的对象 , 第二个参数对对象中的每一个元素 执行 function函数 ,function 的第一个参数 i 是索引,item 是 循环对象中的每一个元素。

    一般的写法:  for(var i in userList){}   这里面的 i 是 索引。

    如果循环对象是 字典,上面的 i, item 分别是 key , value 。

    2、 如果有一个 table 包含多个 tr , 每个 tr 包含多个 td , td 中 有 checkbox 。如果要循环 checkbox 。可以用以下的写法:

    function ReverseAll(){

         $('table :checkbox').each(function(){

           var isChecked = $(this).prop('checked');     // $(this) 获取当前的循环对象,此例中表示 table 中的 checkbox 子元素 集合中的一个元素

           if(isChecked) { $(this).prop("checked",false); }

           else{ $(this).prop("checked",true);  }

      }) ;

    }                                                      //此函数实现对 table表中所有的checkbox 反选功能。

      此处用 $('table :checkbox') 选择器 获取的 table 对象中的所有 checkbox 子元素,并且对每个子元素 执行 function 。是一种链式编程。

  • 相关阅读:
    第三篇:python函数
    第二篇:数据类型
    第一篇:初识python
    PyTorch教程之Autograd
    PyTorch教程之Tensors
    如何解决Python.h:No such file or directory
    如何解决conda install:command not found问题
    Linux 安装Anaconda 4.4.0
    Linux安装pytorch的具体过程以及其中出现问题的解决办法
    Writing Science 7.10 (The Opening and The Funnel)
  • 原文地址:https://www.cnblogs.com/weapon-liu/p/7282833.html
Copyright © 2011-2022 走看看