2014-12-30 39 views
0

迭代我在我的jQuery插件以下代碼:寫重新可用的各功能,通過所有的元素

return this.each(function(){ 
     $(this).removeAttr('src'); 
     new_src = generate_src(); 
     $(this).attr('src' , new_src); 
    }); 

現在我有,檢查是否SRC屬性的特定元素存在於陣列的功能,如果沒有它會將當前元素SRC到陣列中,讓我告訴你這個函數:

function get_prop(current){ 
     var current_src = current.attr('src'); 
     if ($.inArray(current_src , src_storage) === -1) { 
      src_storage.push(current_src); 
     } 
     return src_storage.length; 
    } 

現在假設我在做插件如下:

return this.each(function(){ 
     total_images = get_prop($(this)); // this is the new line added , which calls the get_prop function 
     $(this).removeAttr('src'); 
     new_src = generate_src(); 
     $(this).attr('src' , new_src); 
    }); 

通知我已經如何添加下列行:

total_images = get_prop($(this)); 

問題這種方法

的每個功能實際上是通過在時間的每個元素,這意味着當我調用下面迭代每個函數內部功能:

total_images = get_prop($(this)); 

它將執行只對當前元素和不是移動到下一個元素的集合,啥子我實際上想要它做的是,通過選定元素的整套集合,返回計數並執行餘下的剩餘代碼集。

所以我做了什麼

我採取了以下做法,

this.each(function(){ 
     total_images = get_prop($(this)); 
    }); 


    return this.each(function(){ 
     $(this).removeAttr('src'); 
     new_src = generate_src(); 
     $(this).attr('src' , new_src); 
    }); 

    // function to check for images that maybe repeated . 
    function get_prop(current){ 
     var current_src = current.attr('src'); 
     if ($.inArray(current_src , src_storage) === -1) { 
      src_storage.push(current_src); 
     } 
     return src_storage.length; 
    } 

通知我如何添加下面的一行代碼:

this.each(function(){ 
      total_images = get_prop($(this)); 
     }); 

現在作品就好,它做我想做的事情,IE,

爲所有選定的元素調用get_prop函數。但不知何故,這似乎是一個冗餘的代碼片段,不是很整齊,我不確定一個經驗豐富的程序員是否會採取這種方法,我對Jquery來說是新手,我真的很喜歡如果我可以寫一個函數,它內部有一個遍歷所有元素的函數,但我不知道該怎麼做。

+0

爲什麼看起來這個問題是繼續這個問題:http://stackoverflow.com/q/27687044/1355315? – Abhitalks

+0

@abhitalks這個問題值得一個獨立的問?我感覺它是,所以我問。我是否足夠清楚我的問題,如果沒有,我不介意編輯,我也有一個解決方案,我會嘗試實施,如果成功地在這裏分享它作爲答案,我不指望redy在stackoverflow上做了一些烘焙的代碼,總之,我想到了它的意思,它寫了一個接受2個參數的函數,要迭代的元素和兩個元素,迭代的元素執行的函數,如果這聽起來含糊不清,請等待同時,直到我有時間發佈我的答案。 –

+1

夠公平的。 (1)你是否想通過「generateSource」函數作爲參數? (2)你在用「total_images」做什麼?你的代碼似乎沒有使用它。您是否想從插件中返回一個用於主腳本的總計? – Abhitalks

回答

0

嗯,我會嘗試讓我的回答不是我的問題更加清晰,

首先,什麼我的問題:

我的問題是與每一個功能和espically的事實,它遍歷一次一個元素。讓我告訴你一個例子:

return this.each(function(){ 

       $(this).callsomefunction(); 
       // call some more functions 
       // some more set of instructions 
     }); 

現在看着上面的一段代碼,想什麼我真正想要的是,每個函數執行下面的代碼行:

$(this).callsomefunction(); 

所有選擇元素,然後再轉到下一組指令。現在我該怎麼做?

@abhitalks提供這實際上解決了我的問題小提琴:fiddle

這裏是我的簡單版本:

我有以下功能:

function get_prop(current){ 
      var current_src = current.attr('src'); 
      if ($.inArray(current_src , src_storage) === -1) { 
       src_storage.push(current_src); 
      } 
      return src_storage; 
     } 

現在我想編寫一個函數當被調用時將爲所有選定的元素執行上述功能。

,所以我寫了以下功能:現在

var execute_function = function(element , function_name){ 
      element.each(function(){ 
       val = function_name; 
      }); 
      return val; 
     } 

,最後每個功能裏面,我有這樣的:

return this.each(function(){ 
       store_val = execute_function($(this) , get_prop($(this))); 
       store_val_length = store_val.length; 
       $(this).removeAttr('src'); 
       new_src = generate_src(); 
       $(this).attr('src' , new_src);   
     }); 

使代碼在總看起來像這樣:

var execute_function = function(element , function_name){ 
      element.each(function(){ 
       val = function_name; 
      }); 
      return val; 
     } 


     return this.each(function(){ 

       store_val = execute_function($(this) , get_prop($(this))); 
       store_val_length = store_val.length; 
       $(this).removeAttr('src'); 
       new_src = generate_src(); 
       $(this).attr('src' , new_src);   
     }); 

     // function to check for images that maybe repeated . 
     function get_prop(current){ 
      var current_src = current.attr('src'); 
      if ($.inArray(current_src , src_storage) === -1) { 
       src_storage.push(current_src); 
      } 
      return src_storage; 
     } 
相關問題