How does Ajax render to the page after fetching the data?

Source: Internet
Author: User

About, this problem! Have not found a suitable answer on the Internet (perhaps this problem is silly, hey). Today I used several ways to say:

The first type:

The common thing is to stitch the string directly and then insert it into the element.

var html= ' <li> ' + data.num + ' </li><li> ' + data.floor + ' </li><li> ' + data.name + ' </li><li> ' + Data.money + ' </li> '; elem.innerhtml=html;

The second type:

With the first approximation, first create the object and then add it to the outer object

var node=document.createelement ("LI");
var textnode=document.createtextnode ("Water");
Node.appendchild (Textnode);
document.getElementById ("MyList"). appendchild (node);

The third type:

Can be used on-line MVVM framework, data binding such as: angular, vue and so on here do not give examples of haha

The fourth type:

Examples of templates (template.js)

I'll write a little example myself, don't squirt me.

<! DOCTYPE html>varViewcommand =function() {                varTPL ={product: ["<div>",                        ",                        "<p>{#text #}</p>",                        "</div>"].join (""), title: ["<div class= ' title ' >",                        "<div class= ' main ' >",                        ' ,                        ' <p>{#tips #}</p> ',                        "</div>",                        "</div>",].join ("")                }; varhtml = ""; varFormatestring =function(str, obj) {returnStr.replace (/{\# (\w+) #\}/g,function(match, key) {Console.log (match+Obj[key])returnObj[key];                })                }; varAction ={create:function(data, view) {if(data.length) { for(vari = 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 functionExcute (msg) {Msg.param= Tostring.call (Msg.param) = = "[Object Array]"?Msg.param: [Msg.param];                Action[msg.command].apply (Action, Msg.param);            }            }(); varTit ={tips:"Whispering is.", Title:"An County Arthas",            }            varProduct = [{                "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>

I understand that basically, you can talk about your own, and welcome to ask questions

How does Ajax render to the page after fetching the data?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.