【问题标题】:jQuery toggle with cookies: how to get collapsed state by default and remain accessible?jQuery 与 cookie 切换:如何在默认情况下获得折叠状态并保持可访问性?
【发布时间】:2010-11-23 16:00:44
【问题描述】:

我是一个完全的初学者,所以请原谅我无法看到一个明显的解决方案(如果有的话)。也就是说,我已经在互联网上搜索了这个问题的答案,但只遇到了同样的问题。到目前为止我所做的工作:使用我在http://www.tobypitman.com/multiple-collapsable-panels-with-cookies/ 找到的内容,我设法让多个容器在display:blockdisplay:none 之间切换,并且我正在使用Klaus Hartl 的cookie.js 设置cookie。

一切都非常好!除了我想要关闭切换容器的初始状态。我真的很想在没有任何display:none 的情况下直接在 CSS 中完成此操作,因此在关闭 JS 的情况下仍可访问内容。我不是程序员,我在这里和那里改变事物直到发生某些事情的蛮力方法并没有完全削减它。我在下面包含了 HTML、CSS 和 jQuery - 我的示例中唯一缺少的是用作触发器的 <h6> 的 CSS 图像精灵。




    
    Toggle with cookie

<style>
    .toggle-wrapper {
        overflow:hidden;
        display:block;
    }

    .toggle-wrapper .toggle-container {
        position:relative;
        overflow: hidden;
    }

    .toggle-wrapper h6.trigger {
        background: transparent url(images/trigger-sprite.png) no-repeat left top;/*sprite is 15x30px - plus sign on top, minus on bottom*/
        height: 15px;/*half of sprite's height*/
        cursor:pointer;
        padding:0 0 0 16px;
        margin:0;
    }

    .toggle-wrapper h6.active {
        background-position: left bottom;/*this is the open state, showing the minus sign part of sprite*/
        padding:0 0 0 16px;
    }
</style>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
    /**
     * Get the value of a cookie with the given key.
     *
     * @example $.cookie('the_cookie');
     * @desc Get the value of a cookie.
     *
     * @param String key The key of the cookie.
     * @return The value of the cookie.
     * @type String
     *
     * @name $.cookie
     * @cat Plugins/Cookie
     * @author Klaus Hartl/klaus.hartl@stilbuero.de
     */
    jQuery.cookie = function (key, value, options) {

        // key and value given, set cookie...
        if (arguments.length > 1 && (value === null || typeof value !== "object")) {
            options = jQuery.extend({}, options);

            if (value === null) {
                options.expires = -1;
            }

            if (typeof options.expires === 'number') {
                var days = options.expires, t = options.expires = new Date();
                t.setDate(t.getDate() + days);
            }

            return (document.cookie = [
                encodeURIComponent(key), '=',
                options.raw ? String(value) : encodeURIComponent(String(value)),
                options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
                options.path ? '; path=' + options.path : '',
                options.domain ? '; domain=' + options.domain : '',
                options.secure ? '; secure' : ''
            ].join(''));
        }

        // key and possibly options given, get cookie...
        options = value || {};
        var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
        return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
    };

    // http://www.tobypitman.com/multiple-collapsable-panels-with-cookies/

    $(document).ready(function(){

        $("div.toggle-wrapper h6").addClass("active");

        var l = $('div.toggle-wrapper h6').length;
        var panel = $("div.toggle-wrapper div.toggle-container");

        for (c=0;c<=l;c++){
            var cvalue = $.cookie('panel' + c);
            if ( cvalue == 'closed' + c ) {
                $(panel).eq(c).css({display:"none"});
                $(panel).eq(c).prev().removeClass('active').addClass('inactive');
            };
        };

        $("div.toggle-wrapper h6.active").toggle(
            function () {
                var num = $("div.toggle-wrapper h6").index(this);
                var cookieName = 'panel' + num;
                var cookieValue = 'closed' + num;
                $(this).next("div.toggle-container").slideUp(500);
                $(this).removeClass('active');
                $.cookie(cookieName, cookieValue, { path: '/', expires: 10 }); 
            },
            function () {
                var num = $("div.toggle-wrapper h6").index(this);
                var cookieName = 'panel' + num;
                $(this).next("div.toggle-container").slideDown(500);
                $(this).addClass("active");        
                $.cookie(cookieName, null, { path: '/', expires: 10 });
            }
        );

        $("div.toggle-wrapper h6.inactive").toggle(
            function () {
                var num = $("div.toggle-wrapper h6").index(this);
                var cookieName = 'panel' + num;
                $(this).next("div.toggle-container").slideDown(500);
                $(this).addClass("active");
                $(this).removeClass('inactive');       
                $.cookie(cookieName, null, { path: '/', expires: 10 });
            },
            function () {
                var num = $("div.toggle-wrapper h6").index(this);
                var cookieName = 'panel' + num;
                var cookieValue = 'closed' + num;
                $(this).next("div.toggle-container").slideUp(500);
                $(this).removeClass('active');
                $.cookie(cookieName, cookieValue, { path: '/', expires: 10 }); 
            }
        );

    });
</script>

触发器 1

东西进去了

更多的东西

更均匀

触发器 2

东西进去了

更多的东西

更均匀

触发器 3

东西进去了

更多的东西

更均匀

【问题讨论】:

  • 关闭 JS 后你希望发生什么?
  • 他依靠 JS 向有视力的用户展示折叠的面板;但他希望屏幕阅读器用户仍然可以访问这些面板的内容。如果面板的初始状态是display: none,并且用户是屏幕阅读器用户,并且JS被禁用,那么盲人用户将无法打开面板。
  • 不管怎样,您链接到的页面的 keyboard 可访问性很差;作为一个有视力的键盘用户,当我四处移动时,我根本无法分辨焦点在哪里。 style.css 有一个可怕的 {outline-style:none} 规则,它删除了默认的焦点矩形 - 并且没有用替代品替换它 - 请不要复制这个错误!要么单独保留大纲样式,要么为重点项目提供自己的亮点。此外,请确保您提供键盘奇偶校验 - 如果您在鼠标悬停时执行某些操作(例如突出显示),请考虑在键盘焦点上也执行等效操作。
  • 这里真正的问题不在于屏幕阅读器;但是如何使页面可供 任何 类禁用 JS 的用户访问,无论他们是使用屏幕阅读器还是出于其他原因禁用它。最近的调查显示,大约 98% 的屏幕阅读器确实使用 JS;所以不要假设禁用 JS 意味着它是屏幕阅读器用户;它可能是一个有视力的用户,由于其他原因禁用了 JS。请参阅brucelawson.co.uk/2011/javascript-and-screenreaders 了解一些有趣的上下文。

标签: jquery cookies jquery-plugins accessibility toggle


【解决方案1】:

要创建关闭的默认状态,只需使切换容器依赖于另一个名为“让我们说”“打开”的 cookie。如果没有 cookie 'open + c',容器也会被隐藏。

if ( cvalue == 'closed' + c || cvalue != 'open' + c ){
  //hide()etc...
}

现在不要让切换功能删除 slideDown 上的面板 cookie,而是将面板 cookie 设置为值 'open + num'。

var cookieValue = 'open' + num; 

$.cookie(cookieName, cookieValue, { path: '/', expires: 10 });

设置打开的 cookie 后,脚本会像以前一样工作。这是具有不同类名的全部内容,但这应该不是问题。

$(document).ready(function(){

// Toggle sliding containers with cookies
$("div.toggle_widget h2.toggle_trigger").addClass("active");

var l = $('div.toggle_widget h2.toggle_trigger').length;

var panel = $("div.toggle_widget div.toggle_container");

for (c=0;c<=l;c++){

   var cvalue = $.cookie('panel' + c);

   if ( cvalue == 'closed' + c || cvalue != 'open' + c ) {

        $(panel).eq(c).css({display:"none"});
        $(panel).eq(c).prev().removeClass('active').addClass('inactive');
   };
};


$("div.toggle_widget h2.toggle_trigger.active").toggle(
      function () {
            var num = $("div.toggle_widget h2.toggle_trigger").index(this);
            var cookieName = 'panel' + num;
            var cookieValue = 'closed' + num;
        $(this).next("div.toggle_container").slideUp(250);
        $(this).removeClass('active');
            $.cookie(cookieName, cookieValue, { path: '/', expires: 10 });  

      },
      function () {
            var num = $("div.toggle_widget h2.toggle_trigger").index(this);
            var cookieName = 'panel' + num;
            var cookieValue = 'open' + num;
            $(this).next("div.toggle_container").slideDown(250);
            $(this).addClass("active");         
        $.cookie(cookieName, cookieValue, { path: '/', expires: 10 });
      }
    );


$("div.toggle_widget h2.toggle_trigger.inactive").toggle(
      function () {
            var num = $("div.toggle_widget h2.toggle_trigger").index(this);
            var cookieName = 'panel' + num;
            var cookieValue = 'open' + num;
            $(this).next("div.toggle_container").slideDown(250);
            $(this).addClass("active"); 
            $(this).removeClass('inactive');        
        $.cookie(cookieName, cookieValue, { path: '/', expires: 10 });

      },
      function () {
            var num = $("div.toggle_widget h2.toggle_trigger").index(this);
            var cookieName = 'panel' + num;
            var cookieValue = 'closed' + num;
        $(this).next("div.toggle_container").slideUp(250);
        $(this).removeClass('active');
            $.cookie(cookieName, cookieValue, { path: '/', expires: 10 });  
      }
    );
});

【讨论】:

    【解决方案2】:

    您要解决的问题是如何在视觉上隐藏内容而不使屏幕阅读器无法访问。有很多方法可以做到这一点;最新的(截至 2011 年 7 月)是剪辑方法。这里:

    .hidden {
        position: absolute !important;
        clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
        clip: rect(1px, 1px, 1px, 1px);
        padding: 0 !important;
        border: 0 !important;
        height: 1px !important;
        width: 1px !important;
        overflow: hidden;
    }
    

    如果您愿意,可以在设计节上的文章When and How to Visually Hide Content 中阅读更多相关信息,其中也讨论了一些较旧的方法。

    另外,请注意 slideUp jQuery 方法在完成时将 display: none 分配给它的目标,这将使它对屏幕阅读器不可用,直到面板再次显示。在您的具体情况下,这可能不是问题。但是,如果它打扰您,您可以向 slideUp 添加一个回调函数,它将显示重置为阻止并添加隐藏类,以便在面板离开屏幕后,它再次可供屏幕阅读器使用。

    【讨论】:

    • 这里要注意的一点是 display:block 的内容仍然可以通过键盘进行选项卡;如果有视力的键盘用户使用标签进入这些“隐藏”块之一并且无法再看到键盘焦点消失的位置,这可能会使页面无法访问。
    猜你喜欢
    • 2021-06-08
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多