ajax擷取資料後怎麼去渲染到頁面?

來源:互聯網
上載者:User

標籤:

     關於,這個問題呢!一直沒有在網上找到一個合適答案(可能這問題比較傻,嘿嘿)。今天把自己常用幾種方式說下:

     第一種:

     比較常見的就是直接把字串拼接,然後插入到元素中。

     

var html=‘<li>‘ + data.num + ‘</li><li>‘ + data.floor + ‘</li><li>‘ + data.name + ‘</li><li>‘ + data.money + ‘</li>‘;elem.innerHTML=html;

     第二種:

     與第一種大致,先建立對象,然後添加到外層對象中

var node=document.createElement("LI");
var textnode=document.createTextNode("Water");
node.appendChild(textnode);
document.getElementById(
"myList").appendChild(node);

    第三種:

    可以用網上mvvm架構,資料繫結比如:angular ,vue等 這裡不舉例子了 哈哈

  

    第四種:

    模板的比如 (template.js)

 

    我自己寫個小例子,不要噴我啊

    

<!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <title></title>    </head>    <body>        <div id="con"></div>        <script>            var viewCommand = function() {                var tpl = {                    product: ["<div>",                        "<img src={#srcs#} />",                        "<p>{#text#}</p>",                        "</div>"                    ].join(""),                    title: [                        "<div class=‘title‘>",                        "<div class=‘main‘>",                        ‘<h2>{#title#}</h2>‘,                        ‘<p>{#tips#}</p>‘,                        "</div>",                        "</div>",                    ].join("")                };                var html = "";                var formateString = function(str, obj) {                    return str.replace(/{\#(\w+)#\}/g, function(match, key) {                        console.log(match + obj[key])                        return obj[key];                    })                };                var Action = {                    create: function(data, view) {                        if(data.length) {                            for(var i = 0; i < data.length; i++) {                                html += formateString(tpl[view], data[i])                            }                        } else {                            html += formateString(tpl[view], data);                        }                    },                    display: function(conta, data, view) {                        if(data) {                            this.create(data, view)                        }                        document.getElementById(conta).innerHTML = html;                        html = "";                    }                };                return function excute(msg) {                    msg.param = toString.call(msg.param) == "[object Array]" ? msg.param : [msg.param];                    Action[msg.command].apply(Action, msg.param);                }            }();            var tit = {                tips: "颯颯颯颯是",                title: "安縣阿薩斯",            }            var product = [{                "srcs": "../images/qq.png",                "text": "qqqq"            }, {                "srcs": "../images/weixin.png",                "text": "qq"            }]            viewCommand({                command: "create",                param: [ tit, "title"]            })            viewCommand({                command: "create",                param: [ product, "product"]            })            viewCommand({                command: "display",                param: ["con"]            })        </script>    </body></html>

 

    我瞭解大致就這些,大家可以說說自己的,同時歡迎提問題 

ajax擷取資料後怎麼去渲染到頁面?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.