描述:
用jquery实现数据的遍历
实现:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="node_modules/jquery/dist/jquery.js"></script>
</head>
<body>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<script>
var goodsData=[
{id:1001,icon:"img/a.jpg",
info:"東格 P20 4R/6 230g高光面照片纸/喷墨打印相片纸/相纸 100张装",
initPrice:"¥24.9",nowPrice:"¥7.9",soldOut:0.64},
{id:1002,icon:"img/b.jpg",
info:"1.5米*1.8米宝宝爬行垫加厚爬爬垫防潮泡沫地垫婴儿童游戏地毯家用客厅",
initPrice:"¥59.9",nowPrice:"¥18.8",soldOut:0.87},
{id:1003,icon:"img/c.jpg",
info:"【钜惠疯抢!足量一盒50贴】【买三送一,6盒一疗程】正宗艾草养生足",
initPrice:"¥99",nowPrice:"¥29.9",soldOut:0.95},
{id:1004,icon:"img/d.jpg",
info:"Jackscool加士酷投影仪家用办公全高清1080p智能无线手机投影机4K激",
initPrice:"¥2698",nowPrice:"¥1099",soldOut:0.22}
];
/*
* $.each 遍历,第一个参数是要遍历的对象或者数组
* 第二个参数是一个回调函数,这个函数中也有两个参数
* 如果是数组,这两个参数分别代表下标和值
* 如果是对象,这两个参数分别代表key和值
* 返回原数组或者原对象,中间不使用return
* 回调函数中的this是item
*
* 除了可以遍历数组和对象,也可以遍历jQuery对象
*
* jQuery对象.each(function(){
* ..内容
* })
* 这是遍历jQuery对象
* */
/*var arr= $.each(goodsData,function (index,item) {
console.log(this,index,item);
});
console.log(arr);*/
/*var obj=goodsData[0];
$.each(obj,function (key,value) {
console.log(this,key,value);
})*/
// $.each(goodsData,(index,item)=>console.log(item));
// $.each($("span"),(index,item)=>$(item).text(index));
/*$.each($("span"),function (index,item) {
$(item).text(index);
})*/
/*$("span").each(function (index,item) {
$(item).text(index);
})*/
// $("span").each((index,item)=>{$(item).text(index);console.log(this)});
</script>
</body>
</html>