让我更多地了解您的问题,然后我可能会改进我的答案,但您可以使用此 js 从服务器以 json 格式获取响应并仅更新 html 页面的某些元素/* 为数据中名为“选择器”的每个键刷新 DOM字典 */
/* both data and data['selectors'] need to be json objects
*/
var getJSON, refreshPage,
__slice = [].slice;
refreshPage = function() {
var callback, callbacks, content, data, dom, selector, selectors, _i, _len, _results;
data = arguments[0], callbacks = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
selectors = data['selectors'];
for (selector in selectors) {
content = selectors[selector];
if (!(content === '' || content === void 0)) {
dom = $.toDom(selector);
setText(dom, content, true);
}
}
_results = [];
for (_i = 0, _len = callbacks.length; _i < _len; _i++) {
callback = callbacks[_i];
if (isFunction(callback)) {
_results.push(callback(data));
} else {
_results.push(callback);
}
}
return _results;
};
/* jquery getJSON function simplified
*/
/* calls the callback function with retrieved data automatically
*/
getJSON = function() {
var args, callback, data, url;
url = arguments[0], data = arguments[1], callback = arguments[2], args = 4 <= arguments.length ? __slice.call(arguments, 3) : [];
return $.getJSON(url, data, function(data, status) {
if (isFunction(callback)) {
return callback.apply(null, [data].concat(__slice.call(args)));
}
});
};