Ajax dynamically loading HTML modules

Source: Internet
Author: User

In the development of Web page work, we will often encounter a thing, multiple pages of one or several parts are the same, how to reuse these parts to save development time, in the asp,jsp development has the concept of reference modules, now we use AJAX to implement the module reference, see a piece of HTML code

We can wrap the public part with a custom label that is not in HTML and then, when the page's document is loaded, traverse the custom tab, access the corresponding module via the custom tag's address property, and replace itself, we can modify the HTML structure to the following

Then the JS code, in order to facilitate my direct use of the jquery library

$ (function () {    var tpllist = $ ("TPL");    for (var i = 0; i < tpllist.length; i++) {        var TPL = $ (tpllist[i]);        var src = tpl.attr ("src");        (function (TPL) {            $.get (src, function (data) {                tpl.replacewith (data);            });        }) (TPL);}    );

The closure is used here to protect the variables, the knowledge of closures in the park has a lot of explanation, here do not repeat.

 

Ajax dynamically loading HTML modules

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.