没有标准处理程序的 jQuery 可排序手风琴

发布于 2024-11-30 14:20:04 字数 783 浏览 2 评论 0原文

我目前正在开发一个 Web 应用程序,该应用程序在 jScrollPane 内具有动态更新的手风琴。

我现在正在尝试实现 jQuery Sortable,但没有运气。

目前我发现的示例要求您将手风琴的标题标签更改为“> div > h3”...然后可排序的处理程序是 h3 标签。

当保留手风琴的原始标题并尝试进行可排序工作时,我似乎找不到任何解决方案。

到目前为止,在项目中,我无法更改手风琴的标题,因为许多功能将被破坏并且需要进行大量调整。

有人对这项工作有什么想法吗?

谢谢,

大卫

编辑:这是我试图使其工作的示例代码。我删除了手风琴中的标题选项,并使用可排序处理程序和单击操作。

    var stop = false;
    $("#accordion h3").click(function(event) {
        if (stop) {
            event.stopImmediatePropagation();
            event.preventDefault();
            stop = false;
        }
    });

    $("#accordion").accordion().sortable({
        axis: "y",
        handle: "h3",
        stop: function() {
            stop = true;
        }
    });

I have currently been working on a web application which features a dynamically updating accordion inside a jScrollPane.

I am now trying to implement jQuery Sortable without luck.

Currently examples I have found require you to change the header tags of the accordion to "> div > h3" ...where then sortable's handler is the h3 tag.

When keeping the original headers of the accordion and trying to make sortable work, I can not seem to find any solution.

This far in the project, I can't change the header of the accordion because a lot of the functionality will be broken and will require extensive tweaking.

Does anyone have any ideas on making this work?

Thanks,

David

EDIT: Here is the example code I tried to make work. I removed the header option in the accordion and played around with the sortable handler and the click action.

    var stop = false;
    $("#accordion h3").click(function(event) {
        if (stop) {
            event.stopImmediatePropagation();
            event.preventDefault();
            stop = false;
        }
    });

    $("#accordion").accordion().sortable({
        axis: "y",
        handle: "h3",
        stop: function() {
            stop = true;
        }
    });

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

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

发布评论

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

评论(1

っ左 2024-12-07 14:20:04

您的代码(包括 jsfiddle.net 链接中的代码)与 jQuery 站点上的标准示例之间有两个主要区别。首先(可能是让你绊倒的原因),每个手风琴“成员”都包裹在自己的 div 中。也就是说,如果您有一个指定为折叠容器的 div,然后是 h3、div、h3、div...,则需要折叠容器 和每个成员的容器(因此折叠 div > > )。成员 div > h3/内容 div)。

示例中的标题选项详细说明了我所描述的结构。但是,如果您更改结构并省略容器(我相信 sortable 需要定义应该拖/放的内容),您的手风琴可能仍然可以工作,但现在不可排序。

这是一个正常工作示例。 HTML:

<div id="accordion">
    <div>
        <h3><a href="#">Section 1</a></h3>
        <div>
            <p>
                Text.
            </p>
        </div>
    </div>
    <div>
        <h3><a href="#">Section 2</a></h3>
        <div>
            <p>
                Text.
            </p>
        </div>
    </div>
    <div>
        <h3><a href="#">Section 3</a></h3>
        <div>
            <p>
                Text.
            </p>
            <ul>
                <li>One</li>
                <li>Two</li>
                <li>Three</li>
            </ul>
        </div>
    </div>
    <div>
        <h3><a href="#">Section 4</a></h3>
        <div>
            <p>
                Text.
            </p>
            <p>
                Text.
            </p>
        </div>
    </div>
</div>

JS:

var stop = false;
$("#accordion h3").click(function(event) {
    if (stop) {
        event.stopImmediatePropagation();
        event.preventDefault();
        stop = false;
    }
});
$("#accordion").accordion({
    header: "> div > h3"
    })
    .sortable({
    axis: "y",
    handle: "h3",
    stop: function() {
        stop = true;
    }
});

如果您现在更改结构(保留“手风琴成员容器”以进行排序) ),它应该可以正常工作。在此示例中,我删除了 h3 标签并在手风琴触发器元素内制作了锚点。我还为触发元素提供了一个独特的“句柄”类,以将它们与成员中的任何锚点区分开来(无疑有更好的方法使用 jQuery 选择器来执行此操作,但我生疏且懒惰):

<div id="accordion">
    <div>
        <a href="#" class="handle">Section 1</a>
        <div>
            <p>
                Text...
            </p>
        </div>
    </div>
    ...
</div>

然后我更改了 JS 以反映这。手风琴设置指定锚标记的路径(经过所需的 div 容器)。可排序设置使用我们特殊的“a.handle”,因此只有“handle”类锚标记将充当可排序的句柄:

var stop = false;
$("#accordion h3").click(function(event) {
    if (stop) {
        event.stopImmediatePropagation();
        event.preventDefault();
        stop = false;
    }
});
$("#accordion").accordion({
    header: "> div > a"
    })
    .sortable({
    axis: "y",
    handle: "a.handle",
    stop: function() {
        stop = true;
    }
});

希望有所帮助。

There are two major differences between your code (including what you have in the jsfiddle.net link) and the standard example on jQuery's site. First (and probably what's tripping you up), each accordion "member" is wrapped in its own div. That is, where you have a div designated as the accordion container, then h3, div, h3, div..., you need the accordion container and a container for each member (so accordion div > member divs > h3/content div).

The header option in the example is spelling out the structure I described. However, if you change the structure and leave out the container (which I believe sortable needs to define what should be drag/dropped), your accordion probably still works but now is not sortable.

Here's a normal working example. The HTML:

<div id="accordion">
    <div>
        <h3><a href="#">Section 1</a></h3>
        <div>
            <p>
                Text.
            </p>
        </div>
    </div>
    <div>
        <h3><a href="#">Section 2</a></h3>
        <div>
            <p>
                Text.
            </p>
        </div>
    </div>
    <div>
        <h3><a href="#">Section 3</a></h3>
        <div>
            <p>
                Text.
            </p>
            <ul>
                <li>One</li>
                <li>Two</li>
                <li>Three</li>
            </ul>
        </div>
    </div>
    <div>
        <h3><a href="#">Section 4</a></h3>
        <div>
            <p>
                Text.
            </p>
            <p>
                Text.
            </p>
        </div>
    </div>
</div>

The JS:

var stop = false;
$("#accordion h3").click(function(event) {
    if (stop) {
        event.stopImmediatePropagation();
        event.preventDefault();
        stop = false;
    }
});
$("#accordion").accordion({
    header: "> div > h3"
    })
    .sortable({
    axis: "y",
    handle: "h3",
    stop: function() {
        stop = true;
    }
});

If you change the structure now (leaving in the "accordion member containers" for sortable), it should work fine. In this example, I removed the h3 tags and made the anchors inside the accordion trigger element. I also gave the trigger elements a distinct "handle" class to distinguish them from any anchors within the members (there's undoubtedly a better way to use jQuery selectors to do this but I'm rusty and lazy):

<div id="accordion">
    <div>
        <a href="#" class="handle">Section 1</a>
        <div>
            <p>
                Text...
            </p>
        </div>
    </div>
    ...
</div>

Then I changed the JS to reflect this. The accordion settings designate the path (past the needed div container) to the anchor tag. The sortable settings use our special "a.handle" so only "handle" class anchor tags will act as the sortable's handle:

var stop = false;
$("#accordion h3").click(function(event) {
    if (stop) {
        event.stopImmediatePropagation();
        event.preventDefault();
        stop = false;
    }
});
$("#accordion").accordion({
    header: "> div > a"
    })
    .sortable({
    axis: "y",
    handle: "a.handle",
    stop: function() {
        stop = true;
    }
});

Hope that helps.

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