gpt4 book ai didi

javascript - jquery - 根据文档宽度折叠面板

转载 作者:行者123 更新时间:2023-12-02 15:18:29 27 4
gpt4 key购买 nike

如果文档宽度< 514,我想缩小面板。我的脚本不起作用:

    jQuery(document).ready(function($){

if ($(document).width() < 514) {

if (!$('.panel-heading').hasClass('panel-collapsed')) {
// collapse the panel
$(this).parents('.panel').find('.panel-body').slideUp();
$(this).addClass('panel-collapsed');
$(this).find('i').removeClass('glyphicon-chevron-up').addClass('glyphicon-chevron-down');
}

};
});

以及正在运行并允许用户手动折叠/展开面板的脚本:

jQuery(function ($) {
$('.panel-heading span.clickable').on("click", function (e) {
if ($(this).hasClass('panel-collapsed')) {
// expand the panel
$(this).parents('.panel').find('.panel-body').slideDown();
$(this).removeClass('panel-collapsed');
$(this).find('i').removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-up');
}
else {
// collapse the panel
$(this).parents('.panel').find('.panel-body').slideUp();
$(this).addClass('panel-collapsed');
$(this).find('i').removeClass('glyphicon-chevron-up').addClass('glyphicon-chevron-down');
}
});
});

和 HTML:

<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Panel 1</h3>
<span class="pull-right clickable"><i class="glyphicon glyphicon-chevron-up"></i></span>
</div>

<div class="panel-body"> Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae.
</div>

</div>
</div>

谢谢,贾斯汀.

最佳答案

您应该替换用于 click 事件的选择器的 this 引用。

jQuery(document).ready(function($){

if ($(document).width() < 514) {
var tar = $('.panel-heading span.clickable');
if (!$('.panel-heading').hasClass('panel-collapsed')) {
// collapse the panel
tar.parents('.panel').find('.panel-body').slideUp();
tar.addClass('panel-collapsed');
tar.find('i').removeClass('glyphicon-chevron-up').addClass('glyphicon-chevron-down');
}

};

});

关于javascript - jquery - 根据文档宽度折叠面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34276039/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com