jquery.ui.accordion的修改(支持展开多个)

简介: 原文: jquery.ui.accordion的修改(支持展开多个) 背景:原jquery.ui.accordion插件,最多只能展开一个,不能展开多个,后来在网上找到了一个基于它的一个修改版(https://code.google.com/p/jquery-multi-open-accordion/),但使用后发现它给Array加了一个方法(Array.prototype.hasObject),这样就导致了一个问题:在其它页面使用js遍历一个数组的时候,发现此数组多了一个hasObject值。
原文: jquery.ui.accordion的修改(支持展开多个)

背景:原jquery.ui.accordion插件,最多只能展开一个,不能展开多个,后来在网上找到了一个基于它的一个修改版(https://code.google.com/p/jquery-multi-open-accordion/),但使用后发现它给Array加了一个方法(Array.prototype.hasObject),这样就导致了一个问题:在其它页面使用js遍历一个数组的时候,发现此数组多了一个hasObject值。故做了下修改,希望写JS的prototype不要乱用!!!!!!!!!!!!!

修改版如下:

注:$('#multiOpenAccordion').multiAccordion("option", "active", "all");用set的方式写,all和none才会和效!此插件怎么用,请参考jquery UI文档。

 

(function ($) {

    $.widget('ui.multiAccordion', {
        options: {
            active: 0,
            showAll: null,
            hideAll: null,
            _classes: {
                accordion: 'ui-accordion ui-widget ui-helper-reset ui-accordion-icons',
                h3: 'ui-accordion-header ui-helper-reset ui-state-default ui-corner-all',
                div: 'ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom',
                divActive: 'ui-accordion-content-active',
                span: 'ui-icon ui-icon-triangle-1-e',
                stateDefault: 'ui-state-default',
                stateHover: 'ui-state-hover'
            }
        },

        _create: function () {
            var self = this,

			options = self.options,

			$this = self.element,

			$h3 = $this.children('h3'),

			$div = $this.children('div');

            $this.addClass(options._classes.accordion);

            $h3.each(function (index) {
                var $this = $(this);
                $this.addClass(options._classes.h3).prepend('<span class="{class}"></span>'.replace(/{class}/, options._classes.span));
                if (self._isActive(index)) {
                    self._showTab($this)
                }
            }); // end h3 each

            $this.children('div').each(function (index) {
                var $this = $(this);
                $this.addClass(options._classes.div);
            }); // end each

            $h3.bind('click', function (e) {
                // preventing on click to navigate to the top of document
                e.preventDefault();
                var $this = $(this);
                var ui = {
                    tab: $this,
                    content: $this.next('div')
                };
                self._trigger('click', null, ui);
                if ($this.hasClass(options._classes.stateDefault)) {
                    self._showTab($this);
                } else {
                    self._hideTab($this);
                }
            });


            $h3.bind('mouseover', function () {
                $(this).addClass(options._classes.stateHover);
            });

            $h3.bind('mouseout', function () {
                $(this).removeClass(options._classes.stateHover);
            });

            // triggering initialized
            self._trigger('init', null, $this);

        },

        // destroying the whole multi open widget
        destroy: function () {
            var self = this;
            var $this = self.element;
            var $h3 = $this.children('h3');
            var $div = $this.children('div');
            var options = self.options;
            $this.children('h3').unbind('click mouseover mouseout');
            $this.removeClass(options._classes.accordion);
            $h3.removeClass(options._classes.h3).removeClass('ui-state-default ui-corner-all ui-state-active ui-corner-top').children('span').remove();
            $div.removeClass(options._classes.div + ' ' + options._classes.divActive).show();
        },

        // private helper method that used to show tabs
        _showTab: function ($this) {
            var $span = $this.children('span.ui-icon');
            var $div = $this.next();
            var options = this.options;
            $this.removeClass('ui-state-default ui-corner-all').addClass('ui-state-active ui-corner-top');
            $span.removeClass('ui-icon-triangle-1-e').addClass('ui-icon-triangle-1-s');
            $div.slideDown('fast', function () {
                $div.addClass(options._classes.divActive);
            });
            var ui = {
                tab: $this,
                content: $this.next('div')
            }
            this._trigger('tabShown', null, ui);
        },

        // private helper method that used to show tabs 
        _hideTab: function ($this) {
            var $span = $this.children('span.ui-icon');
            var $div = $this.next();
            var options = this.options;
            $this.removeClass('ui-state-active ui-corner-top').addClass('ui-state-default ui-corner-all');
            $span.removeClass('ui-icon-triangle-1-s').addClass('ui-icon-triangle-1-e');
            $div.slideUp('fast', function () {
                $div.removeClass(options._classes.divActive);
            });
            var ui = {
                tab: $this,
                content: $this.next('div')
            }
            this._trigger('tabHidden', null, ui);
        },

        // helper method to determine wether passed parameter is an index of an active tab or not
        _isActive: function (num) {
            var options = this.options;
            // if array
            if (typeof options.active == "boolean" && !options.active) {
                return false;
            } else {
                if (options.active.length != undefined) {
                    for (var i = 0; i < options.active.length; i++) {
                        if (options.active[i] == num)
                            return true;
                    }
                } else {
                    return options.active == num;
                }
            }
            return false;
        },

        // return object contain currently opened tabs
        _getActiveTabs: function () {
            var $this = this.element;
            var ui = [];
            $this.children('div').each(function (index) {
                var $content = $(this);
                if ($content.is(':visible')) {
                    //ui = ui ? ui : [];
                    ui.push({
                        index: index,
                        tab: $content.prev('h3'),
                        content: $content
                    });
                }
            });
            return (ui.length == 0 ? undefined : ui);
        },

        getActiveTabs: function () {
            var el = this.element;
            var tabs = [];
            el.children('div').each(function (index) {
                if ($(this).is(':visible')) {
                    tabs.push(index);
                }
            });
            return (tabs.length == 0 ? [-1] : tabs);
        },

        // setting array of active tabs
        _setActiveTabs: function (tabs) {
            var self = this;
            var $this = this.element;
            if (typeof tabs != 'undefined') {
                $this.children('div').each(function (index) {
                    var $tab = $(this).prev('h3');
                    if (jQuery.inArray(index, tabs) != -1) {
                        self._showTab($tab);
                    } else {
                        self._hideTab($tab);
                    }
                });
            }
        },

        // active option passed by plugin, this method will read it and convert it into array of tab indexes
        _generateTabsArrayFromOptions: function (tabOption) {
            var tabs = [];
            var self = this;
            var $this = self.element;
            var size = $this.children('h3').size();
            if ($.type(tabOption) === 'array') {
                return tabOption;
            } else if ($.type(tabOption) === 'number') {
                return [tabOption];
            } else if ($.type(tabOption) === 'string') {
                switch (tabOption.toLowerCase()) {
                    case 'all':
                        var size = $this.children('h3').size();
                        for (var n = 0; n < size; n++) {
                            tabs.push(n);
                        }
                        return tabs;
                        break;
                    case 'none':
                        tabs = [-1];
                        return tabs;
                        break;
                    default:
                        return undefined;
                        break;
                }
            }
        },

        // required method by jquery ui widget framework, used to provide the ability to pass options
        // currently only active option is used here, may grow in the future
        _setOption: function (option, value) {
            $.Widget.prototype._setOption.apply(this, arguments);
            var el = this.element;
            switch (option) {
                case 'active':
                    this._setActiveTabs(this._generateTabsArrayFromOptions(value));
                    break;
                case 'getActiveTabs':
                    var el = this.element;
                    var tabs;
                    el.children('div').each(function (index) {
                        if ($(this).is(':visible')) {
                            tabs = tabs ? tabs : [];
                            tabs.push(index);
                        }
                    });
                    return (tabs.length == 0 ? [-1] : tabs);
                    break;
            }
        }

    });
})(jQuery);


 

目录
相关文章
|
6天前
|
人工智能 数据可视化 安全
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
本文详解如何用阿里云Lighthouse一键部署OpenClaw,结合飞书CLI等工具,让AI真正“动手”——自动群发、生成科研日报、整理知识库。核心理念:未来软件应为AI而生,CLI即AI的“手脚”,实现高效、安全、可控的智能自动化。
34422 16
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
|
18天前
|
人工智能 JSON 机器人
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
本文带你零成本玩转OpenClaw:学生认证白嫖6个月阿里云服务器,手把手配置飞书机器人、接入免费/高性价比AI模型(NVIDIA/通义),并打造微信公众号“全自动分身”——实时抓热榜、AI选题拆解、一键发布草稿,5分钟完成热点→文章全流程!
45254 142
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
|
8天前
|
人工智能 JSON 监控
Claude Code 源码泄露:一份价值亿元的 AI 工程公开课
我以为顶级 AI 产品的护城河是模型。读完这 51.2 万行泄露的源码,我发现自己错了。
4760 20
|
1天前
|
人工智能 自然语言处理 安全
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
本文介绍了Claude Code终端AI助手的使用指南,主要内容包括:1)常用命令如版本查看、项目启动和更新;2)三种工作模式切换及界面说明;3)核心功能指令速查表,包含初始化、压缩对话、清除历史等操作;4)详细解析了/init、/help、/clear、/compact、/memory等关键命令的使用场景和语法。文章通过丰富的界面截图和场景示例,帮助开发者快速掌握如何通过命令行和交互界面高效使用Claude Code进行项目开发,特别强调了CLAUDE.md文件作为项目知识库的核心作用。
1109 5
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
|
6天前
|
人工智能 API 开发者
阿里云百炼 Coding Plan 售罄、Lite 停售、Pro 抢不到?最新解决方案
阿里云百炼Coding Plan Lite已停售,Pro版每日9:30限量抢购难度大。本文解析原因,并提供两大方案:①掌握技巧抢购Pro版;②直接使用百炼平台按量付费——新用户赠100万Tokens,支持Qwen3.5-Max等满血模型,灵活低成本。
1659 5
阿里云百炼 Coding Plan 售罄、Lite 停售、Pro 抢不到?最新解决方案

热门文章

最新文章