javascript - 网站动态内容

标签 javascript jquery html performance

有没有可以用来提供 HTML 内容的 JavaScript 库?我想创建一个内容可以动态更改的DIV。

<div id="header">
  <h2>Header here</h2>
</div>

<div id="dynamic-content">
  <p>-- Dynamic content here --</p>
</div>

<div id="footer">
  <h3>Footer</h3>
</div>

我计划为此使用 jQuery,只需从服务器检索 HTML 标记,然后使用 jQuery 的 html()函数替换 #dynamic-content 的内容。我只是好奇如果检索到的 HTML 标记“很大”(比如说整篇文章),这是否实用。

是否有一个简单的 JavaScript 模板框架可以实现这一点?或者我什至可以使用模板 JS 框架吗?

最佳答案

--助手--

var uiHelper = function () {

    var htmls = {};

    var getHTML = function (options) {
        /// <summary>Returns HTML in a string format</summary>
        /// <param name="options" type="object">options{url:The url to the file with the HTML,successCallback:function,errorCallback:function,isAsync:true||false,cache:true|false}</param>

        function xhrSuccess() {
            if (this.cache) { htmls[this.url] = this.responseText; };

            if (this.successCallback) {
                this.successCallback.apply(this.responseText, this.arguments);
            } else {
                return htmls[this.url];
            };
        };
        function xhrError() {

            if (this.errorCallback) {
                this.errorCallback.apply(this.statusText);
            } else {
                return this.statusText;
            };
        };

        if (!htmls[options.url]) {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", options.url, options.isAsync);
            xmlhttp.cache = options.cache || false;
            xmlhttp.url = options.url;
            xmlhttp.onload = xhrSuccess;
            xmlhttp.onerror = xhrError;
            xmlhttp.successCallback = options.successCallback || undefined;
            xmlhttp.errorCallback = options.errorCallback || undefined;
            xmlhttp.send();
        } else {

            if (options.successCallback) {
                options.successCallback.apply(htmls[options.url], this.arguments);
            } else {
                return htmls[options.url];
            };
        };
    };

    return {
        getHTML: getHTML
    };
}();

--用法---

    uiHelper.getHTML({
        url: url, isAsync: true, cache: false, successCallback: function () {

            document.getElementById('dynamicContent').innerHTML = this;
        }
    });

--您的 HTML---

<div id="header">
  <h2>Header here</h2>
</div>

<div id="dynamic-content">
  <p id='dynamicContent'>-- Dynamic content here --</p>
</div>

<div id="footer">
  <h3>Footer</h3>
</div>

关于javascript - 网站动态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21896700/

相关文章:

javascript - 从 Node JS 服务器 net::ERR_CONNECTION_RESET 上传视频时出错

javascript - 从第一个默认选择选项触发更改事件

javascript - 格式化从 JSON 文件接收数据的 React 表

java - Jsoup 遍历 DOM 树的问题

javascript - 分配处理成本与条件

javascript - 可以使用 websharper 作为 JS 的替代品吗?

javascript - 如何识别 Firefox 中的 http 请求是谁发起的?

javascript - 通过使用 Jquery 插入 javascript 加载 LinkedIn 成员(member)个人资料

Jquery 在单击标题时选择取消选中复选框

html - CSS图像文本叠加到图像中心和图像框阴影奇怪