简体   繁体   English

jQuery可折叠tabindex

[英]jQuery Collapsible tabindex

Using jquery.collapsible.js I notice that the tabindex does not change when expanded/closed. 使用jquery.collapsible.js时,我注意到tabindex在展开/关闭时不会改变。 I nearly have a solution but would appreciate it if someone could improve on this piece of code as I am sure there is a better way to do this. 我几乎有一个解决方案,但是如果有人可以改进这段代码,我将不胜感激,因为我确信有更好的方法可以做到这一点。

$('.collapsible').each(function() {
 $('.collapsible').on('click',function(e) {
 if($('div').hasClass('collapsible collapse-open')) {
$('.collapsible.collapse-open').attr("tabIndex", 0);
 } else {
$('.collapsible.collapse-close').attr("tabIndex", -1);
}

});

});

The problem is the tabindex only changes on the second click and then the 0,-1 order is wrong. 问题是tabindex仅在第二次单击时发生更改,然后0,-1顺序错误。

If you're just looking for a better way, and your solution already works, you could simplify it with this. 如果您只是在寻找一种更好的方法,并且您的解决方案已经可以使用,则可以使用此方法进行简化。 You don't need to loop through each .collapsible with the each function just to add the click event handler. 您无需仅通过添加click事件处理程序就可以遍历每个.collapsibleeach函数。

$(".collapsible").on('click', function (e) {
    var self = $(this);
    var tabIndex = self.hasClass("collapse-open") ? 0 : -1;
    self.attr("tabIndex", tabIndex); 
});

This will select any element with the class collapsible , bind the click event, and then check if that element has the collapse-open class to determine which tab index to apply. 这将选择具有collapsible类的任何元素,绑定click事件,然后检查该元素是否具有collapse-open类,以确定要应用的选项卡索引。

Thanks to Ihan for the more efficient script. 感谢Ihan提供更有效的脚本。 It is working now I just had to change the default class from 'collapse-open' to 'collapse-close'. 现在可以正常工作了,我只需要将默认类从“ collapse-open”更改为“ collapse-close”。

$(".collapsible").on('click', function (e) {
    var self = $(this);
    var tabIndex = self.hasClass("collapse-close") ? 0 : -1;
    self.attr("tabIndex", tabIndex); 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM