使用带有奇怪标记的 jQuery 手风琴

发布于 2024-07-06 18:51:51 字数 1211 浏览 7 评论 0原文

我有这个 HTML 结构,想将其转换为手风琴。

<div class="accor">
    <div class="section">
        <h3>Sub section</h3>
        <p>Sub section text</p>
    </div>
    <div class="section">
        <h3>Sub section</h3>
        <p>Sub section text</p>
    </div>
    <div class="section">
        <h3>Sub section</h3>
        <p>Sub section text</p>
    </div>
</div>

基本上使用 h3 作为折叠标题,每个 div.section 中的其余内容作为每个折叠面板的内容。 (另请注意:标题可以是 h2 和 h6 之间的任何内容,具体取决于它们的嵌套)。

我认为如果重构 DOM 树,使 h3 位于每个 div 之外,这将是最简单的,因为这就是手风琴默认的工作方式:

    <h3>Sub section</h3>
    <div class="section">
        <p>Sub section text</p>
    </div>

唯一的问题是:如何移动标题? (我无权更改 HTML)。

var $sections = $("div.accor > .section"),
    $headings = $sections.find("> :header")
;
// I figured that inserting each heading to be before its parent might
// be the answer:
$headings.insertBefore($headings.find(":parent"));

// ... but that doesn't do anything

I have this HTML structure and want to convert it to an accordion.

<div class="accor">
    <div class="section">
        <h3>Sub section</h3>
        <p>Sub section text</p>
    </div>
    <div class="section">
        <h3>Sub section</h3>
        <p>Sub section text</p>
    </div>
    <div class="section">
        <h3>Sub section</h3>
        <p>Sub section text</p>
    </div>
</div>

Basically using the h3s as accordion headers, and the rest of the content in each div.section as the content for each accordion panel. (Also note: the headings could be anything between h2 and h6, depending on their nesting).

I figured that this would be easiest if the DOM tree were restructured so the h3s were outside each div since that's how the accordion works by default:

    <h3>Sub section</h3>
    <div class="section">
        <p>Sub section text</p>
    </div>

The only problem is: how to move the headings around? (I don't have access to change the HTML).

var $sections = $("div.accor > .section"),
    $headings = $sections.find("> :header")
;
// I figured that inserting each heading to be before its parent might
// be the answer:
$headings.insertBefore($headings.find(":parent"));

// ... but that doesn't do anything

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

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

发布评论

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

评论(2

独夜无伴 2024-07-13 18:51:51

啊,我找到了解决方案。

使用 $.each()

$headings.each(function(i, el) {
    var $this = $(el), $p = $this.parent();
    $this.insertBefore($p);
});

有比这更好的解决方案吗? 也许只是使用香草手风琴选项?

Ah, I found the solution.

Using $.each()

$headings.each(function(i, el) {
    var $this = $(el), $p = $this.parent();
    $this.insertBefore($p);
});

Is there a better solution than this, though? Perhaps just using the vanilla Accordion options?

月下客 2024-07-13 18:51:51

这个怎么样:

$('.accor .section').each(function() {
    $('h3', this).insertBefore($(this));
});

How about this:

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