2017-04-08 09:59:40

把json图片信息展示到页面

如数据库返回格式为

 var datas = {
            count: 3,
            imgs: [
                'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2905893513,2695425341&fm=23&gp=0.jpgg',
                'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3633691784,3186862163&fm=23&gp=0.jpg',
                'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3706772667,1323205698&fm=23&gp=0.jpg'
            ]
        }

如何把这些信息以图片的方式展现到 页面上呢。
直接上源码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        img {
            width: 200px;
            margin-left: 10px;
        }
    </style>
</head>

<body>
    <!--图片容器-->
    <div id="container">
        <img src="">
    </div>
    <script>
        //借用百度图片
        //服务器返回如下格式数据
        //如何展示到页面上
        var datas = {
            count: 3,
            imgs: [
                'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2905893513,2695425341&fm=23&gp=0.jpgg',
                'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3633691784,3186862163&fm=23&gp=0.jpg',
                'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3706772667,1323205698&fm=23&gp=0.jpg'
            ]
        }

        //准备容器
        var arr = [];
        //拼接图片标签
        for (var i = 0; i < datas.count; i++) {
            arr.push('<img src="' + datas.imgs[i] + '">');
        }
        //展现在容器中
        document.querySelector('#container').innerHTML = arr.join('');
    </script>
</body>

</html>

效果图

alt

页面元素

alt

本文链接:https://blog.zxysilent.com/post/json-imgs-html.html

-- EOF --

Comments