[修改] $.fn.loadCache 更新


涅魂
 
文章: 4463
註冊時間: 2004-01-04 11:17 am
來自: Taiwan
性別: 男生

[修改] $.fn.loadCache 更新

文章涅魂 » 2011-01-17 4:04 pm

這裡將$.fn.loadCache更新,除了原有功能外,並加上兩個功能
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);
         });
      }
   },



以上,如有缺漏或任何問題請提出~~


圖檔


ETERNAL
 
文章: 2937
註冊時間: 2003-12-03 11:08 pm
性別: 男生

Re: [修改] $.fn.loadCache 更新

文章ETERNAL » 2011-01-18 10:42 am

剛才測了沒什麼問題


水色論壇 http://www.et99.net
簡恩峻分享


回到 最果物語 - LOST Island

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 6 位訪客

cron