从非标准 HTML 结构创建 jQuery Accordion
我们都知道您使用此结构来制作手风琴,
<div id="accordion">
<h3><a href="#">First header</a></h3>
<div>First content</div>
<h3><a href="#">Second header</a></h3>
<div>Second content</div>
</div>
但是如果您想使用由糟糕的 CMS 设置的现有代码来创建手风琴,该怎么办?
这是我几乎可以让它工作的布局
<div id="sideNavContent" class="clearfix">
<ul>
<!-- Left Nav (selected) level: 2 - Not hidden - with children -->
<li class="NML3Selected"><div><a id="NML3LinkSelected" href="/forcedelivered.aspx" title="Force Delivered">Force Delivered</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/340.aspx" title="Test Page 1">Test Page 1</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/341.aspx" title="Test Page 2">Test Page 2</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/342.aspx" title="Test Page 3">Test Page 3</a></div></li>
<li class="NML3"><div><a id="NML3Link" href="/ftmfaq.aspx" title="Force Training Frequently Asked Questions">FAQs</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/340.aspx" title="Test Page 1">Test Page 4</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/341.aspx" title="Test Page 2">Test Page 5</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/342.aspx" title="Test Page 3">Test Page 6</a></div></li>
<li class="NML3"><div><a id="NML3Link" href="/trainingsupport.aspx" title="Training Support">Training Support</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/340.aspx" title="Test Page 1">Test Page 7</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/341.aspx" title="Test Page 2">Test Page 8</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/342.aspx" title="Test Page 3">Test Page 9</a></div></li>
</ul>
</div>
,它只输出手风琴中的第一个子链接,我
<script type="text/javascript">
$(function() {
$("#sideNavContent").accordion({
collapsible: true,
active: false,
header: "li.NML3",
//header: "li:has(NML3)", //argh *sobs*
navigation: true
})
});
</script>
正在使用 jQuery WAS 命令,放弃了它(第一个标题作为 .NL3Selected 类,所以我希望手风琴看到它包含 NL3)
任何聪明的 jQueryness 欢迎,制作这个可怕的标记,作为手风琴工作..
p
We all know that you use this structure to make an accordion
<div id="accordion">
<h3><a href="#">First header</a></h3>
<div>First content</div>
<h3><a href="#">Second header</a></h3>
<div>Second content</div>
</div>
But what if you want to create an accordion, using the code already in place, set by a terrible CMS?
This is the layout
<div id="sideNavContent" class="clearfix">
<ul>
<!-- Left Nav (selected) level: 2 - Not hidden - with children -->
<li class="NML3Selected"><div><a id="NML3LinkSelected" href="/forcedelivered.aspx" title="Force Delivered">Force Delivered</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/340.aspx" title="Test Page 1">Test Page 1</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/341.aspx" title="Test Page 2">Test Page 2</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/342.aspx" title="Test Page 3">Test Page 3</a></div></li>
<li class="NML3"><div><a id="NML3Link" href="/ftmfaq.aspx" title="Force Training Frequently Asked Questions">FAQs</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/340.aspx" title="Test Page 1">Test Page 4</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/341.aspx" title="Test Page 2">Test Page 5</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/342.aspx" title="Test Page 3">Test Page 6</a></div></li>
<li class="NML3"><div><a id="NML3Link" href="/trainingsupport.aspx" title="Training Support">Training Support</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/340.aspx" title="Test Page 1">Test Page 7</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/341.aspx" title="Test Page 2">Test Page 8</a></div></li>
<li class="NML4"><div><a id="NML4Link" href="/342.aspx" title="Test Page 3">Test Page 9</a></div></li>
</ul>
</div>
I can ALMOST get it to work, it outputs only the first child link in an acordion, im using
<script type="text/javascript">
$(function() {
$("#sideNavContent").accordion({
collapsible: true,
active: false,
header: "li.NML3",
//header: "li:has(NML3)", //argh *sobs*
navigation: true
})
});
</script>
Been messing about with the jQuery WAS command, gave up on that (the first heading as a class of .NL3Selected so i wanted accordion to see that it contained NL3)
Any clever jQueryness welcome, to make this horrible markup, work as an accordion..
p
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能会编写自己的手风琴小部件。
不过,您可以尝试在折叠式
change
事件上应用函数,并迭代下一个元素,直到遇到另一个标头元素。然后将这些元素包装到
中,看看手风琴是否能正常工作。
I probably would just write my own accordion widget.
Although, you could try applying a function on the accordion
change
event and iterate over the next<li>
elements until you hit another header element. Then wrap those elements into a<div>
and see if the accordion will work properly.您可以预处理 html 并将其更改为您想要的内容,如下所示:
You could pre-process the html and change it to what you want it to be like this: