简体   繁体   English

如何通过Ajax加载HTML模块?

[英]How to load html modules via ajax?

Depending upon a path that a user enters my site through I want to load the correct module? 根据用户要通过其进入我的网站的路径来加载正确的模块?

Is this as simple as storing the HTML in a file called some_file.htm then ajaxing that file to the client than appending it to the DOM? 这就像将HTML存储在一个名为some_file.htm文件中,然后将该文件与客户端进行some_file.htm而不是将其附加到DOM一样简单吗?

I'm wondering if it is that easy or if there are some caveats I'm not aware of? 我想知道这是否那么容易,或者是否有一些我不知道的警告?

Thanks! 谢谢!

Simple example: 简单的例子:

JavaScript: JavaScript:

<script defer="true">
    (function(){

        var xhr = new XMLHttpRequest();

        xhr.addEventListener("load", function(resp){
            document.querySelector('div.container').innerHTML = resp.target.responseText;
        }, false);

        xhr.open("get", "some_file.htm", true);
        xhr.send();
    }());
</script>

HTML: HTML:

<div class="container"></div>

Documentation: 说明文件:

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM