ajax 动态添加商品列表

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8" />
<title></title>

 

//自定义模板
<script type="text/template" id="app_tpl">
<div class="product">
<div class="imageContainer">
<img src="{#img#}" />
<div class="layer">
<p>找同款</p>
<p>找相似</p>
</div>
</div>
<div class="concent">
<p class="price">
<span class="price-text">¥{#price#}</span>
<span class="sum">{#sales#}人付款</span>
</p>
<p class="title">{#title#}</p>
<p class="stroe">
<span class="stroe-title">{#store#}</span>
<span class="adress">{#adress#}</span>
</p>
</div>
</div>
</script>
<style type="text/css">
* {
margin: 0;
padding: 0;
}

#app {
width: 1088px;
margin: 50px auto;
font-family: "微软雅黑";
}

.product {
width: 250px;
height: 360px;
float: left;
border: 1px solid #ccc;
margin: 10px;
}

.product:hover{
border: 1px solid #555B24;
}
.imageContainer{
position: relative;
width: 250px;
height: 250px;
}
.product:hover .layer {
display: block;
}
.layer{
height:40px ;
background: #F55B24;
position: absolute;
bottom: 0;
left: 0;
display: none;
}
.layer p {
width: 125px;
float: left;
line-height: 30px;
color: #fff;
font-size: 12px;
text-align: center;
}

.content {
padding: 10px;
}

.content .price {
width: 100%;
line-height: 40px;
height: 40px;
}
.price{
height: 30px;
}
.price .price-text {
color: #F55B24;
font-size: 20px;
float: left;
font-weight: bold;
}
.price .price-text span{
color: #fff;
font-size:12px;
background: #F55B24;
font-weight: 400;
vertical-align: top;
}
.price .sum {
color: #999;
font-size: 12px;
display: inline;
float: right;
}

.title {
color: #666;
font-size: 12px;
}

.stroe {
width: 100%;
color: #999;
font-size: 12px;
line-height: 30px;
}

.stroe .store-title {

float: left;
}

.stroe .adress {
float: right;
}
</style>
</head>

<body>
<div id="app"></div>
</body>

//js
<script type="text/javascript">
//js命名空间 定义一个工具方法库
var Util = {
//获取获取元素数
id: function(id) {
return document.getElementById(id);
},
//封装ajax方法
ajax: function(url, callback) {
//创建XMLHttpRequest对象
var xhr = new XMLHttpRequest();
//订阅事件
xhr.onreadystatechange = function() {
if(xhr.readyState === 4) {
if(xhr.status === 200) {
var data = JSON.parse(xhr.responseText);
callback && callback(data);
}
}
}
xhr.open("get", url, true);
xhr.send(null);
}



}
//格式化字符串
function formDataString(str,data){
return str.replace(/\{#(\w+)#\}/g,function(match,$1){
// console.log(this,1) //this作用域
// console.log(arguments,2)//参数列表
return data[$1]===undefined?"":data[$1];
});
}
var tpl = Util.id("app_tpl").innerHTML;

var html="";

Util.ajax("data/list.json",function(data) {
var data=data.list;
for(var i=0;i<data.length;i++){
html+=formDataString(tpl,data[i])
}

Util.id('app').innerHTML=html;

})
</script>

</html>

//list.json

{
"errno": 0,
"list": [
{
"img": "images/1.jpg",
"price": "798.00",
"sales": "8",
"title": "帛薇儿2017春装新款女装条纹拼接连衣裙蝴蝶结中长款真丝连衣裙 ",
"store": "pokwai服饰旗舰店",
"adress": "广东 深圳"
},
{
"img": "images/2.jpg",
"price": "168.00",
"sales": "2946",
"title": "加棉毛呢短外套短款刺绣棒球服女秋冬季2016新款潮加厚含羊毛呢子 ",
"store": "妍蝶旗舰店",
"adress": "浙江 杭州"
},
{
"img": "images/3.jpg",
"price": "699.00",
"sales": "974",
"title": "【多件多折】ochirly欧时力翻领羊毛呢大衣长外套1YY3345910 ",
"store": "ochirly官方旗舰店",
"adress": "广东 广州"
},
{
"img": "images/4.jpg",
"price": "19.90",
"sales": "584",
"title": "秋冬季女装新款2016黑色打底衫女长袖t恤白色纯棉上衣修身衣服潮 ",
"store": "娇人圣典旗舰店",
"adress": "福建 厦门"
},
{
"img": "images/5.jpg",
"price": "38.89",
"sales": "430",
"title": "新款韩版黑白条纹修身2016长袖T恤女秋打底衫莫代尔大码女装上衣 ",
"store": "xy服饰旗舰店",
"adress": "广东 东莞"
},
{
"img": "images/6.jpg",
"price": "89.90",
"sales": "8637",
"title": "中老年女装冬装棉衣妈妈装羽绒棉服中长款毛领外套老年人加厚棉袄 ",
"store": "胖胖家衣橱",
"adress": "浙江 嘉兴"
},
{
"img": "images/7.jpg",
"price": "69.00",
"sales": "3085",
"title": "秋冬季新款加绒加厚卫衣女装套头宽松韩版潮学生圆领长袖上衣外套 ",
"store": "米子旗旗舰店",
"adress": "广东 广州"
},
{
"img": "images/8.jpg",
"price": "49.90",
"sales": "3733",
"title": "百搭毛衣线衣韩版女装套头打底针织衫短款上衣秋冬季潮外套宽松 ",
"store": "黎雪儿旗舰店",
"adress": "江苏 苏州"
},
{
"img": "images/9.jpg",
"price": "19.00",
"sales": "355",
"title": "中老年女装冬装棉衣妈妈装羽绒棉服中长款毛领外套老年人加厚棉袄 ",
"store": "娟子裤行",
"adress": "上海"
},
{
"img": "images/10.jpg",
"price": "118.00",
"sales": "968",
"title": "2016女装新款秋冬装针织衫女开衫百搭短款毛衣外套女秋冬季韩版潮 ",
"store": "玛思狄拉旗舰店",
"adress": "浙江 嘉兴"
},
{
"img": "images/11.jpg",
"price": "53.00",
"sales": "490",
"title": "2016秋冬季新款娃娃领女装针织衫套头打底衫修身毛衣女短款上衣潮 ",
"store": "诗梵米亚旗舰店",
"adress": "浙江 嘉兴"
},
{
"img": "images/12.jpg",
"price": "28.80",
"sales": "2419",
"title": "秋冬新款女装纯色麻花毛衣女套头韩版圆领短款针织衫上衣打底衫厚 ",
"store": "沐晗旗舰店",
"adress": "江苏 苏州"
}
]
}

posted @ 2017-06-12 09:17  点动  阅读(2203)  评论(0编辑  收藏  举报