jquery 部分展开和折叠

发布于 2024-10-28 19:12:25 字数 1979 浏览 0 评论 0原文

我正在使用 jquery“包含”选择器来确定单击 div 时要执行的操作...我想展开或折叠分面导航的一部分。

然而,看起来“contains”选择器可能只在 DOM 最初加载时查看 div 的内容,而没有看到由早期 jquery 调用换出的较新内容。

我有一个主要功能版本在这里工作...只需要一点点推动即可使顶部部分正确折叠: http ://jsfiddle.net/brianadkins/nAabP/


HTML:

<div class="facetname" id="facetname-fbr">
        Brand
    </div>
    <ul class="facetvalues" id="facetvalues-fbr">
        <li>facet1val1</li>
        <li>facet1val2</li>
        <li>facet1val3</li>
        <li>facet1val4</li>
        <li>facet1val5</li>
        <li>facet1val6</li>
        <li>facet1val7</li>
        <li>facet1val8</li>
        <li>facet1val9</li>
        <li>facet1va10</li>
        <li>facet1val11</li>
        <li>facet1val12</li>
        <li>facet1val13</li>
    </ul>
    <div class="slidermenu" id="slidermenu-fbr">
    </div>

Javascript:

var ListLengthHidingTrigger = 7;
var InitialListItems = 4;

if ($("#facetvalues-fbr li").length > ListLengthHidingTrigger) {
    $("#facetvalues-fbr li:gt("+(InitialListItems-1)+")").hide(); // hide all but first N sections
    $('#slidermenu-fbr').html('Show All Brands');
}
$('div#slidermenu-fbr:contains("All")').click(function() {
                $('#facetvalues-fbr li').show(); // hide all but first 2 sections
                $('#slidermenu-fbr').html('Show Fewer Brands');            
});
$('div#slidermenu-fbr:contains("Fewer")').click(function() {
                $("#facetvalues-fbr li:gt("+(InitialListItems-1)+")").hide(); // hide all but first N sections
                $('#slidermenu-fbr').html('Show More Brands');            
});
$('#facetname-fbr').click(function() {
    $('#facetvalues-fbr').slideToggle(0);
});

I'm using the jquery 'contains' selector to determine what to do when a div is clicked... I want to expand or collapse a section of faceted navigation.

However, It appears the 'contains' selector might only be looking at the contents of the div when the DOM was initially loaded and is not seeing the newer contents that were swapped out by an earlier jquery call.

I have a mostly-functional version working here... just need a little push to get the top section collapsing properly : http://jsfiddle.net/brianadkins/nAabP/


HTML:

<div class="facetname" id="facetname-fbr">
        Brand
    </div>
    <ul class="facetvalues" id="facetvalues-fbr">
        <li>facet1val1</li>
        <li>facet1val2</li>
        <li>facet1val3</li>
        <li>facet1val4</li>
        <li>facet1val5</li>
        <li>facet1val6</li>
        <li>facet1val7</li>
        <li>facet1val8</li>
        <li>facet1val9</li>
        <li>facet1va10</li>
        <li>facet1val11</li>
        <li>facet1val12</li>
        <li>facet1val13</li>
    </ul>
    <div class="slidermenu" id="slidermenu-fbr">
    </div>

Javascript:

var ListLengthHidingTrigger = 7;
var InitialListItems = 4;

if ($("#facetvalues-fbr li").length > ListLengthHidingTrigger) {
    $("#facetvalues-fbr li:gt("+(InitialListItems-1)+")").hide(); // hide all but first N sections
    $('#slidermenu-fbr').html('Show All Brands');
}
$('div#slidermenu-fbr:contains("All")').click(function() {
                $('#facetvalues-fbr li').show(); // hide all but first 2 sections
                $('#slidermenu-fbr').html('Show Fewer Brands');            
});
$('div#slidermenu-fbr:contains("Fewer")').click(function() {
                $("#facetvalues-fbr li:gt("+(InitialListItems-1)+")").hide(); // hide all but first N sections
                $('#slidermenu-fbr').html('Show More Brands');            
});
$('#facetname-fbr').click(function() {
    $('#facetvalues-fbr').slideToggle(0);
});

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

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

发布评论

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

评论(2

匿名的好友 2024-11-04 19:12:25

尝试使用 jQuery live()

http://jsfiddle.net/maniator/nAabP/8/

代码:

var ListLengthHidingTrigger = 7;
var InitialListItems = 4;

if ($("#facetvalues-fbr li").length > ListLengthHidingTrigger) {
    $("#facetvalues-fbr li:gt("+(InitialListItems-1)+")").hide(); // hide all but first N sections
    $('#slidermenu-fbr').html('Show All Brands');
}
$('div#slidermenu-fbr:contains("All")').live('click',function() {
                $('#facetvalues-fbr li').show(); // hide all but first 2 sections
                $('#slidermenu-fbr').html('Show Fewer Brands');            
});
$('div#slidermenu-fbr:contains("Fewer")').live('click',function() {
                $("#facetvalues-fbr li:gt("+(InitialListItems-1)+")").hide(); // hide all but first N sections
                $('#slidermenu-fbr').html('Show All Brands');            
});
$('#facetname-fbr').click(function() {
    $('#facetvalues-fbr').slideToggle(0);
});

try using jQuery live()

http://jsfiddle.net/maniator/nAabP/8/

code:

var ListLengthHidingTrigger = 7;
var InitialListItems = 4;

if ($("#facetvalues-fbr li").length > ListLengthHidingTrigger) {
    $("#facetvalues-fbr li:gt("+(InitialListItems-1)+")").hide(); // hide all but first N sections
    $('#slidermenu-fbr').html('Show All Brands');
}
$('div#slidermenu-fbr:contains("All")').live('click',function() {
                $('#facetvalues-fbr li').show(); // hide all but first 2 sections
                $('#slidermenu-fbr').html('Show Fewer Brands');            
});
$('div#slidermenu-fbr:contains("Fewer")').live('click',function() {
                $("#facetvalues-fbr li:gt("+(InitialListItems-1)+")").hide(); // hide all but first N sections
                $('#slidermenu-fbr').html('Show All Brands');            
});
$('#facetname-fbr').click(function() {
    $('#facetvalues-fbr').slideToggle(0);
});
我们只是彼此的过ke 2024-11-04 19:12:25

你走在正确的道路上。如果您要查找短语“全部”,则需要在“显示所有品牌”和“显示更多品牌”之间做出选择。此外,甚至没有理由将处理程序绑定到同一对象。绑定一个可能更容易,只需检查文本 All 或类似的内容即可。另外,使用 live()

http://jsfiddle.net/Jaybles/nAabP /7/

var ListLengthHidingTrigger = 7;
var InitialListItems = 4;

if ($("#facetvalues-fbr li").length > ListLengthHidingTrigger) {
    $("#facetvalues-fbr li:gt("+(InitialListItems-1)+")").hide(); // hide all but first N sections
    $('#slidermenu-fbr').html('Show All Brands');
}
$('#slidermenu-fbr').live('click',function() {
    if ($(this).text().indexOf('All') >0){
        $('#facetvalues-fbr li').show(); // hide all but first 2 sections
        $('#slidermenu-fbr').html('Show Fewer Brands');            
    }else{
        $("#facetvalues-fbr li:gt("+(InitialListItems-1)+")").hide(); // hide all but first N sections
        $('#slidermenu-fbr').html('Show All Brands');         
    }
});

$('#facetname-fbr').click(function() {
    $('#facetvalues-fbr').slideToggle(0);
});

You are on the right path. You need to decide between "Show All Brands" and "Show More Brands" if you're going to look for the phrase "All". In addition, there's no reason to bind to even handlers to the same object. May be easier to bind one and just do a check for the text All or something to that effect. Also, use live()

http://jsfiddle.net/Jaybles/nAabP/7/

var ListLengthHidingTrigger = 7;
var InitialListItems = 4;

if ($("#facetvalues-fbr li").length > ListLengthHidingTrigger) {
    $("#facetvalues-fbr li:gt("+(InitialListItems-1)+")").hide(); // hide all but first N sections
    $('#slidermenu-fbr').html('Show All Brands');
}
$('#slidermenu-fbr').live('click',function() {
    if ($(this).text().indexOf('All') >0){
        $('#facetvalues-fbr li').show(); // hide all but first 2 sections
        $('#slidermenu-fbr').html('Show Fewer Brands');            
    }else{
        $("#facetvalues-fbr li:gt("+(InitialListItems-1)+")").hide(); // hide all but first N sections
        $('#slidermenu-fbr').html('Show All Brands');         
    }
});

$('#facetname-fbr').click(function() {
    $('#facetvalues-fbr').slideToggle(0);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文