jquery 函数到插件:返回每个结果的问题

发布于 2024-11-01 17:24:06 字数 1838 浏览 5 评论 0原文

我在将下面的函数转换为插件时遇到问题,你能告诉我如何修复它吗?

功能,

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 未定义

什么想法吗?

谢谢。

I am having a problem turning the function below into a plugin, could you please tell me how to fix it?

the function,

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;
}

it works perfectly,

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

the plugin,

(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);

then I will get the error message,

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

index_highest is not defined

Any ideas?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故笙诉离歌 2024-11-08 17:24:06

只需将 var index_highest = 0; 移至 each 语句之外即可:

(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);

Simply move var index_highest = 0; outside of the each statement:

(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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文