2011-04-17 33 views
0

我有一个问题,把下面的功能转换成插件,请问我该如何解决它?jquery功能插件:问题每个返回结果

功能,

function highest_Zindex(){ 

    var index_highest = 0; 

    // more effective to have a class for the div you want to search and 
    // pass that to your selector 
    $("div").each(function() { 

     if($(this).css("zIndex") > 0) 
     { 
      // always use a radix when using parseInt 
      var index_current = parseInt($(this).css("zIndex"), 10); 

      if(index_current > index_highest) { 
       index_highest = index_current; 
      } 
     } 
    }); 

    return index_highest; 
} 

它完美的作品,

var test = highest_Zindex(); 
alert(test); 

插件,

(function($){ 

    $.fn.extend({ 

     get_highest_Zindex: function(options) { 

      var defaults = { 
       increment: 10 // The opacity of the background layer. 
      } 

      var options = $.extend(defaults, options); 


      var $cm = this.each(function(){ 


       var o = options; 
       var object = $(this); // always return #document. 

       var index_highest = 0; 

       if(object.css("zIndex") > 0) 
       { 
        // always use a radix when using parseInt 
        var index_current = parseInt(object.css("zIndex"), 10); 
        //alert(index_current); 

        if(index_current > index_highest) { 
         index_highest = index_current; 
        } 
       } 

      }); 

      return index_highest; 

     } 
    }); 

})(jQuery); 

那么我会得到错误信息,

var test = $("div").get_highest_Zindex(); 
alert(test); 

index_highest没有定义

任何想法?

谢谢。

回答

1

只需移动each声明var index_highest = 0;外:

(function($){ 
    $.fn.extend({ 
     get_highest_Zindex: function(options) { 

      var defaults = { increment: 10 } 
      var options = $.extend(defaults, options); 
      var index_highest = 0;       // <- here 

      var $cm = this.each(function(){ 
       var o = options; 
       var object = $(this); 

       if (object.css("zIndex") > 0) { 
        var index_current = parseInt(object.css("zIndex"), 10); 

        if (index_current > index_highest) { 
         index_highest = index_current; 
        } 
       } 

      }); 

      return index_highest; 
     } 
    }); 
})(jQuery); 
+0

啊!我怎么错过那部分!谢谢rubiii! :-) – laukok 2011-04-17 12:10:16

+0

不客气! – rubiii 2011-04-17 12:21:06