jquery函数到插件:返回每个插件的结果时出现问题
我在将下面的函数转换为插件时遇到问题,你能告诉我如何修复它吗 功能,jquery函数到插件:返回每个插件的结果时出现问题,jquery,jquery-plugins,z-index,each,Jquery,Jquery Plugins,Z Index,Each,我在将下面的函数转换为插件时遇到问题,你能告诉我如何修复它吗 功能, 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).
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);
未定义索引
有什么想法吗
谢谢。只需移动
var index\u 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);
(函数($){
$.fn.extend({
获取最高Zindex:函数(选项){
变量默认值={increment:10}
var options=$.extend(默认值,选项);
var index_highest=0;//0){
var index_current=parseInt(object.css(“zIndex”),10);
如果(当前索引>最高索引){
最高指数=当前指数;
}
}
});
回报指数最高;
}
});
})(jQuery);