jquery 部分展开和折叠
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 jQuery live()
http://jsfiddle.net/maniator/nAabP/8/
代码:
try using jQuery live()
http://jsfiddle.net/maniator/nAabP/8/
code:
你走在正确的道路上。如果您要查找短语“全部”,则需要在“显示所有品牌”和“显示更多品牌”之间做出选择。此外,甚至没有理由将处理程序绑定到同一对象。绑定一个可能更容易,只需检查文本
All
或类似的内容即可。另外,使用live()
http://jsfiddle.net/Jaybles/nAabP /7/
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, uselive()
http://jsfiddle.net/Jaybles/nAabP/7/