1. 如同$.fn.load那樣能附加selector的功能
2. 載入前會修改目標對象的html內容為空值
使用範例 寫:index.html 寫:...
<p id="f1">
my name is john.
</p>
...
運行下列程式碼
- 代碼: 選擇全部
$("body").loadCache("index.html #f1");
會將my name is john.貼入到<body>內,<p></p>並不會被引入
開啟檔案 js/extends.js 寫:Goal: 對$.fn.loadCache做更新
尋找
- 代碼: 選擇全部
loadCache: function(path, callback) {
...
},
覆蓋為
- 代碼: 選擇全部
loadCache: function(path, callback) {
var query = path.indexOf(" ");
if(query >= 0) {
var tmp = $.trim(path.slice(query, path.length));
path = $.trim(path.slice(0, query));
query = tmp;
} else {
query = null;
}
var text = "";
var $targets = this.html("");
//init callback & space
if(callback == undefined) callback = $.noop || new Function("");
if($.fn.loadCache.cache == undefined) $.fn.loadCache.cache = {};
if($.fn.loadCache.cache[path] == undefined) {
//load template & store
$.get(path, function(txt) {
$.fn.loadCache.cache[path] = txt;
text = txt;
_pasteTemplate();
}, "html");
} else {
//use cache
text = $.fn.loadCache.cache[path];
_pasteTemplate();
}
return this;
function _pasteTemplate() {
//do filtering
if(query != null) text = $("<div/>").append(text).find(query).html();
$targets.each(function() {
$(this).html(text);
callback.apply(this);
});
}
},
以上,如有缺漏或任何問題請提出~~