本文实例讲述了jQuery调取jSon数据并展示的方法。分享给大家供大家参考。具体如下:
以下代码是将页面中的展示部分
复制代码 代码如下:
function searchProductlistByfilterCondition(index, type, sort, filterWord) {
//cite_html
var citem_html = '<div class="citem"><div class="citemtop">'
+ '<a href="" target="_blank"><img src="{imgList}" /></a><div class="citemtxt">'
+ '<a class="citemtitle" target="_blank" href="">{title}</a><div class="citemtc">'
+ '<span class="yy-icon yy-time txtellipsis">{time}</span>'
+ '</div></div></div>'
+ '<div class="citemqt">'
+ '<span class="yy-icon yy-view">{mark}</span> <span class="yy-icon yy-comment">{price}</span>'
+ '<a class="yy-icon yy-like" href=""><span>{praise}</span> </a></div>'
+ '<div class="citemqt citemfoot"><div class="citemzl"><a class="citemimg" href="">'
+ '<img width="24" height="24" src="images/main/1408603734394.jpg" />'
+ '<span class="txtellipsis">{user}</span> </a></div>'
+ '<span class="fcfiled">( <a target="_blank" href="">转载</a>- <a target="_blank" href="">'
+ ' 站酷中国</a> )</span>'
+ '<div id="hiddenpopop" class="miniprofile bottom"><div class="mnphead">'
+ '<a target="_blank" href="">'
+ ' <img src="images/main/author.jpg" width="50" height="50" /></a><div><p>'
+ ' <span class="mnpw-1">小海藻</span> <span class="mnpw-2">福建 福州</span> '
+ ' <span class="mnpw-3">人气:<span>256</span></span></p><p class="fmbtn mt10">'
+ ' <a target="_blank" href="" class="ufmbtn ufocus"><span class="yy-icon yy-ufocicon"></span>'
+ ' <span>加关注</span> </a><a target="_blank" href="" class="ufmbtn ml10 ufusm"><span class="yy-icon yy-ufusm">'
+ ' </span><span>发私信</span> </a></p></div></div><div class="cb"></div>'
+ ' <div class="mnpbody mt10">'
+ ' <a target="_blank" href="">'
+ ' <img src="images/main/1.jpg" width="105" height="68" />'
+ ' </a><a target="_blank" href="">'
+ ' <img src="images/main/1.jpg" width="105" height="68" />'
+ ' </a><a target="_blank" href="">'
+ ' <img src="images/main/1.jpg" width="105" height="68" />'
+ ' </a></div></div></div></div>';
$.get('ajax/getProductListByFliterCondition.ashx',
{ pageIndex: index, Type: type, Sorting: sort, keyWord: filterWord }, function (data) {
$.each(data.jsona, function (index, elem) {
citemHtml += citem_html.replace('{imgList}', elem.msg_img_list).replace('{title}', elem.msg_title)
.replace('{time}', elem.msg_date).replace('{mark}', elem.msg_mark)
.replace('{price}', elem.msg_price).replace('{praise}', elem.msg_praise)
.replace('{user}', elem.msg_create_user);
})
$("#fsD1").after(citemHtml);
}, 'json');
//alert(sort);
}
//cite_html
var citem_html = '<div class="citem"><div class="citemtop">'
+ '<a href="" target="_blank"><img src="{imgList}" /></a><div class="citemtxt">'
+ '<a class="citemtitle" target="_blank" href="">{title}</a><div class="citemtc">'
+ '<span class="yy-icon yy-time txtellipsis">{time}</span>'
+ '</div></div></div>'
+ '<div class="citemqt">'
+ '<span class="yy-icon yy-view">{mark}</span> <span class="yy-icon yy-comment">{price}</span>'
+ '<a class="yy-icon yy-like" href=""><span>{praise}</span> </a></div>'
+ '<div class="citemqt citemfoot"><div class="citemzl"><a class="citemimg" href="">'
+ '<img width="24" height="24" src="images/main/1408603734394.jpg" />'
+ '<span class="txtellipsis">{user}</span> </a></div>'
+ '<span class="fcfiled">( <a target="_blank" href="">转载</a>- <a target="_blank" href="">'
+ ' 站酷中国</a> )</span>'
+ '<div id="hiddenpopop" class="miniprofile bottom"><div class="mnphead">'
+ '<a target="_blank" href="">'
+ ' <img src="images/main/author.jpg" width="50" height="50" /></a><div><p>'
+ ' <span class="mnpw-1">小海藻</span> <span class="mnpw-2">福建 福州</span> '
+ ' <span class="mnpw-3">人气:<span>256</span></span></p><p class="fmbtn mt10">'
+ ' <a target="_blank" href="" class="ufmbtn ufocus"><span class="yy-icon yy-ufocicon"></span>'
+ ' <span>加关注</span> </a><a target="_blank" href="" class="ufmbtn ml10 ufusm"><span class="yy-icon yy-ufusm">'
+ ' </span><span>发私信</span> </a></p></div></div><div class="cb"></div>'
+ ' <div class="mnpbody mt10">'
+ ' <a target="_blank" href="">'
+ ' <img src="images/main/1.jpg" width="105" height="68" />'
+ ' </a><a target="_blank" href="">'
+ ' <img src="images/main/1.jpg" width="105" height="68" />'
+ ' </a><a target="_blank" href="">'
+ ' <img src="images/main/1.jpg" width="105" height="68" />'
+ ' </a></div></div></div></div>';
$.get('ajax/getProductListByFliterCondition.ashx',
{ pageIndex: index, Type: type, Sorting: sort, keyWord: filterWord }, function (data) {
$.each(data.jsona, function (index, elem) {
citemHtml += citem_html.replace('{imgList}', elem.msg_img_list).replace('{title}', elem.msg_title)
.replace('{time}', elem.msg_date).replace('{mark}', elem.msg_mark)
.replace('{price}', elem.msg_price).replace('{praise}', elem.msg_praise)
.replace('{user}', elem.msg_create_user);
})
$("#fsD1").after(citemHtml);
}, 'json');
//alert(sort);
}
citem_html:页面代码(关键字用特殊符号和文字表示,例如 {imgList},(time)等)
$.get():从指定的资源请求数据
第一个参数:jSon插件;第二个参数:json数据格式;第三个参数:function(data)方法,data为取得的json数据串
$.each(data.jsona,function(index,elem))
参数说明:
data.jsona:json数据
function(index,elem):
参数说明:
index:索引
elem:相当于json数据串
此后,将html代码添加到页面中。
此方法的作用:
1、传递参数
2、获取
3、展示
接下来在
复制代码 代码如下:
$(function () {
//页面初始化调用
searchProductlistByfilterCondition("", "", "", "");
//查询框,关键字查询--调用函数
$('#search ').click(function () {
pageIndex = 1;
searchProductlistByfilterCondition(pageIndex, sortP, typeL, keyWord);
});
}
//页面初始化调用
searchProductlistByfilterCondition("", "", "", "");
//查询框,关键字查询--调用函数
$('#search ').click(function () {
pageIndex = 1;
searchProductlistByfilterCondition(pageIndex, sortP, typeL, keyWord);
});
}
页面js中,调用,只需要将具体的数据对应填上就可以了。
页面中的js作用:
1、绑定
2、赋值
希望本文所述对大家的jQuery程序设计有所帮助。