<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>$.each遍历数组</title>
<style type="text/css">
*{ margin: 0;padding: 0; }
p{ width: 300px;height: 30px;text-align: center; }
div{ width: 300px;height: 30px;text-align: center; }
</style>
</head>
<body>
<!-- 第1种 -->
<p class="five" data-fal="0">11</p>
<p class="five" data-fal="0">1</p>
<p class="five" data-fal="0">22</p>
<p class="five" data-fal="0">22</p>
<p class="five" data-fal="0">22</p>
<p class="five" data-fal="0">22</p> <!-- 第2种 -->
<div class="one" data="1">标签1</div>
<div class="one" data="2">标签2</div>
<div class="one" data="3">标签3</div>
<div class="one" data="4">标签4</div>
<div class="one" data="5">标签5</div>
<div class="one" data="6">标签6</div>
<div class="one" data="7">标签7</div>
<div class="one" data="8">标签8</div>
<div class="one" data="9">标签9</div>
<div class="one" data="10">标签1</div>
<div class="one" data="20">标签20</div>
<div class="one" data="38">标签38</div>
<div class="one" data="59">标签59</div> </body>
<script src="jquery-1.11.1.min.js"></script>
<script>
// $.each用来遍历数组,解析数据 // 第1种 $.each遍历数组 把数组的内容value 当下标用
var arr1=[1,2,5];
// index是下标,value是数组内容
$.each(arr1,function(index,value){
$('.five').eq(value).attr('data-fal',1).css("background","blue");
}); // 第2种 模拟数据(字符串),通过数据给 带有data属性改样式
var str= 'FFFE030,2,3,4,6,7,8,10,20,22,26,38,39,40,41,59,62,66,68';
var str2 = str.substring(6);
var arr = str2.split(',');
$.each(arr,function (index,value){
if( value>=4 && value<=9){
//1
$("[data="+value+"]").css("background","red");
}
else if( value>=16 && value<=33){
// 2
$("[data="+value+"]").css("background","rgba(45,53,69,0.6)");
}
else if(value>=38 && value<=55) {
// 3
$("[data="+value+"]").css("background","#fe4365");
}
else if ( value >=59) {
// 4
$("[data="+value+"]").css("color","rgb(255, 111, 87)");
} }); // 第三种 遍历数组
var arr2= [5,6,8,10];
$.each(arr2,function(index,value){
console.log("下标:"+index+"值:"+value);
}); // 遍历对象
var json ={ name:"王",age:"20",sex:"女"};
$.each(json,function(index,value){
console.log("名称:"+index+"内容:"+value);
}) </script>
</html>

jq的$.each遍历数组的更多相关文章

  1. *使用while循环遍历数组创建索引和自增索引值

    package com.chongrui.test;/* *使用while循环遍历数组 *  *  * */public class test {    public static void main ...

  2. nodejs 遍历数组的两种方法

    var array = [1,2,3]; array.forEach(function(v,i,a){ console.log(v); console.log(i); console.log(a); ...

  3. 原生js使用forEach()与jquery使用each遍历数组,return false 的区别

    原生js使用forEach()与jquery使用each()遍历数组,return false 的区别: 1.使用each()遍历数组a,如下: var a=[20,21,22,23,24]; $.e ...

  4. js中数组遍历for与for in区别(强烈建议不要使用for in遍历数组)

    js中遍历数组的有两种方式 var array=['a'] //标准的for循环 for(var i=1;i<array.length;i++){ alert(array[i]) } //for ...

  5. 以forin的方式遍历数组时进行删除操作的注意点

    今天在修改某项需求的时候,需要在遍历的时候将匹配项移除掉,采用的时forin的方式遍历,然后运行的时候却crash掉了 for (NSString*str in self.btnArray) { if ...

  6. 使用for循环遍历数组

    package review20140419;/* * 遍历数组 */public abstract class Test1 {    //程序入口    public static void mai ...

  7. php 中遍历数组时使用引用出现的问题

    今天在使用foreach遍历数组时发现,当使用&时会出现问题: $arr = array( array('id' => 100, 'error'=> 'aa'), array('i ...

  8. Java:JSTL遍历数组,List,Set,Map

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  9. js遍历数组的错误方法

    for (var index in myArray) { // don't actually do this console.log(myArray[index]); } 缺点: 数组的索引值inde ...

随机推荐

  1. lesson 23 one man's meat is another man's poison

    lesson 23 one man's meat is another man's poison delicacy n. 美味:佳肴: delicious adj. 美味的:可口的 关于虚拟语气: I ...

  2. 【WXS数据类型】RegExp

    生成 regexp 对象需要使用 getRegExp函数,注意与JS的使用方法不同( new RegExp(pattern,modifiers);) 原型:getRegExp(pattern, mod ...

  3. lintcode112 删除排序链表中的重复元素

    删除排序链表中的重复元素   给定一个排序链表,删除所有重复的元素每个元素只留下一个. 您在真实的面试中是否遇到过这个题? Yes 样例 给出 1->1->2->null,返回 1- ...

  4. python同时遍历两个list

    两个list, 有对应关系,希望同时完成遍历 用迭代器迭代的方法也不是不可以,python提供了更直观的方法: 可以使用zip把两个list打包 , 类似: list1 = [1,2,3,4] lis ...

  5. 使用truffle测试部署合约

    truffle console let contract; contract=BloggerCoin.deployed().then(instance=>contract=instanc e);

  6. MongoDB3.2 集群搭建

    一.集群的理论知识 1.1 集群成员 MongoDB的集群类似于GreenPlum集群,由一个入口节点负责任务分发与结果统计,分片结节负责执行任务.不同GP,多了一个config servers. 集 ...

  7. 深搜(DFS)与广搜(BFS)区别

    最近做了不少的搜索题,时而用到DFS时而用到BFS,这里对两种搜索方法做一个总结. 广度优先搜索算法(Breadth-First-Search,缩写为 BFS),是一种利用队列实现的搜索算法.简单来说 ...

  8. 展示github中的页面(Github Pages)

    一.创建一个仓库,名为"user_name.github.io"(此处user_name替换为你自己的github用户名),并在根目录下创建index.html,则该仓库下的所有h ...

  9. CNN之间的计算

    参考内容 1.网易云课堂微专业——深度学习—04第一周:http://mooc.study.163.com/smartSpec/detail/1001319001.htm 2.CNN基础介绍:http ...

  10. iOS-修改导航栏文字字体和颜色

    //修改导航栏文字字体和颜色 nav.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[RGBColor co ...