请求列表与详情展示

<script type="text/javascript">
        
        function getList(){
            $.ajax({
                type: "GET",
                url: "./api/goodsList.json",
                success: function(data){
                    showList(data);
                },
                error: function(){
                    console.log("err")
                }
            })
        }
        getList();
        function showList(data){
            var data = data.list;
            console.log(data);
            var str = '';
            data.forEach(function(ele,index){
               str += '<a href="goodsInfo.html?id='+ ele.id +'">\
                            <div class="goods_item">\
                                <img src = "'+ ele.imgurl[0] + '" alt = "">\
                                <p class="item_name">'+ ele.name + '</p>\
                                <p class="item_price">'+ ele.spectList[0].price + '</p>\
                            </div>\
                        </a>'
            })
            $('.tab_content').html(str);
        }
    </script>

 

 

详情

function getId() {
    var optionList = location.search.slice(1).split('&');

//jsp   news/show/205.html   获取id=205   location.href.split("/").splice(-1).toString().split(".")[0]
    var idNum;
    optionList.forEach(function (ele, index) {
        if (ele.indexOf('id=') != -1) {
            idNum = ele.slice(3);
        }
    })
    return idNum;
}

console.log(getId());
// 通过ID值取到当前ID数据

getGoodList();
function getGoodList() {
    $.ajax({
        type: 'GET',
        url: 'http://localhost:8080/api/goodsList.json',
        success: function (data) {
            createInfo(data);
            // console.log(data);
        },
        error: function () {
            console.log('error');
        }
    })
}
function createInfo(data) {
    var idNum = getId();
    var dataList = data.list;
    var len = dataList.length;
    var listStr = '';
    var liStr = '';
    for (var i = 0; i < len; i++) {
        if (dataList[i].id == idNum) {
            $('.infor_one_img').html('<img src="' + dataList[i].imgurl[0] + '">');
            $('.one_name').html(dataList[i].name);
            dataList[i].spectList.sort(findPrice('price'));
            $('.one_price').html('¥' + dataList[i].spectList[0].price + '-' + dataList[i].spectList[dataList[i].spectList.length - 1].price);
            dataList[i].imgurl.forEach(function (ele, index) {
                listStr += '<img src="' + ele + '">'
            })
            $('.infor_th').append($(listStr));
            dataList[i].spectList.forEach(function (ele, index) {
                liStr = '<li class="buy_spect_li" data-price="' + ele.price + '">' + ele.spect + '</li>'
            })
            $('.buy_spect_wrap ul').html(liStr);
            $('.price_value').html($('.one_price').html());

        }
    }
}

转载于:https://my.oschina.net/parchments/blog/2252056

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值