Analog Template Replacement function--js

Source: Internet
Author: User

Summary: Because the previous project was angular development (like Vue and react), it was convenient to use two-way data bindings, and then thought about how to use the jquery framework for list generation

Knowledge Point: Regular with its inverse reference, str.replace

HTML section

<!--list li templates -<Scripttype= "text/html"ID= "Row">    <Li>        <h6 class="Qa_title"><a href="Qa_article.shtml?qid={{qid}}">{{title}}</a></h6>        <Div class="Qa_info C">            <span><I class="icon Icon_visit"></i> Browse:{{view_num}}</span>            <span><I class="icon ICON_COMMT"></i> Answer:{{comoment_num}}</span>            <span class="FR"><I class="icon Icon_date"></i> Question Time:{{add_time}}</span>        </div>    </li></Script>

JS section, using "\{\{(. +?)" \}\} "," IgM ", is finding the fields contained in {{}} below $ (' #row '),

Templi.replace (Reg, function (node, key) {                              return data.info[i][key];                       });

To replace a field with content

1Gethostlist:function(){2             varrequest={}3Request.page_type= ' Get_hot_question_list ';4request.page_num= ' 0 ';5 $.ajax ({6url:fields.url+ ' Question ',7 Data:request,8DataType: "JSON",9Type: ' Post ',TenAsyncfalse, OneSuccessfunction(Result) {//Callback after success A                     if(result.ret==0){ -                         varData=Result.info; -                         //List Data the                         varReg =NewRegExp ("\{\{(. +?) \}\} "," IgM ");//matches the contents of {{}} -                         varTempli = $ ("#row"). html (); -                         varHtml= "; -                          for(vari=0;i<data.all_num-1;i++) { +                             if(!data.info[i]) Break; -html+= Templi.replace (Reg,function(node, key) { +                                 returnData.info[i][key]; A                             }); at                         } -$ ("#qa_hot_list"). HTML (HTML); -}Else{ -Func.popshow (' #pop '); -$ (' #pop. Jx_inf '). Text (Result.info) -                     } in                 }, -Errorfunction(e) {//Callback after failure to  +                 } -})

Effect:

Analog Template Replacement function--js

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.