Getting started with JavaScript templates _ javascript tips-js tutorials

Source: Internet
Author: User
I first wrote a rich interactive page. The JavaScript code contains many html tags. For example, when ajax is used to insert a li in a list, I will directly splice the data with the html tag into a complete html and insert it into ul. No matter whether the data is retrieved from the server or from the user's input-either method is the same.
This splicing process is put in a JavaScript file, which is not elegant. If you also put the style in JavaScript, the data, structure, and style are all porridge. To maintain such code, you may want to commit suicide. The most out-of-the-box approach is to put the final HTML generated on the page directly on the server side, and the ajax spit data will contain
  • Labels, such a page has almost no scalability. modifying a foreground style involves modifying the background code.

    Later we learned that we should not customize DOM styles in JavaScript, but just define the corresponding class in the CSS file and use this class in JavaScript.

    What we need to do next is to use a JavaScript template to structure the HTML (in this case, yes
  • Tags) are also separated from JavaScript.

    There are many JavaScript templates on the market. Take handlebars as an example:

    We define a template in the html of the page:

    The Code is as follows:


  • 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.