如何在 JQuery UI Selectable 上实现切换功能?

发布于 2024-11-02 04:26:49 字数 131 浏览 0 评论 0原文

我正在使用 JQuery UI - 可选择。如果该元素已被按下并且已被选择(切换),我想取消选择该元素,

您能帮我添加此功能吗!

I'm using JQuery UI - Selectable. I want to deselect the element if it has been pressed and it has already been selected (toggle)

Could you help me to add this functionality please !

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

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

发布评论

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

评论(3

迷途知返 2024-11-09 04:26:49

由于所有的类回调,你不会做得比这更好:

$(function () {
        $("#selectable").selectable({
            selected: function (event, ui) {
                if ($(ui.selected).hasClass('click-selected')) {
                    $(ui.selected).removeClass('ui-selected click-selected');

                } else {
                    $(ui.selected).addClass('click-selected');

                }
            },
            unselected: function (event, ui) {
                $(ui.unselected).removeClass('click-selected');
            }
        });
    });

Because of all the class callbacks, you're not going to do much better than this:

$(function () {
        $("#selectable").selectable({
            selected: function (event, ui) {
                if ($(ui.selected).hasClass('click-selected')) {
                    $(ui.selected).removeClass('ui-selected click-selected');

                } else {
                    $(ui.selected).addClass('click-selected');

                }
            },
            unselected: function (event, ui) {
                $(ui.unselected).removeClass('click-selected');
            }
        });
    });
鱼忆七猫命九 2024-11-09 04:26:49

如果您在单击时按住 Ctrl 键,则您已经可以通过 jQuery UI 选择该功能。

如果您确实需要将其作为默认功能,则不需要使用 selectable,您可以将其作为简单的 onclick 处理程序来完成,就像 nolabel 建议的那样。

或者...您也可以编辑jquery.ui.selectable.js并添加一个选项来满足您的需要。不应该太难,有 4 个地方检查 event.metaKey ,确保您的选项已设置,只需运行代码路径,就好像 event.metaKey 始终为 true 一样。

由于可选内容可以使用更多自定义功能,您还可以向 jQuery UI 开发人员提出功能请求,将其包含在下一个正式版本中。

You already have that functionality with jQuery UI selectable if you're holding down the Ctrl key while clicking.

If you really need that as the default functionality, you don't need to use selectable, you can do it just as a simple onclick handler, like what nolabel suggested.

Or... you might as well edit jquery.ui.selectable.js and add an option which does just what you need. Shouldn't be too hard, there are 4 places where event.metaKey is checked, make sure if your option is set, just run the codepaths as if event.metaKey was always true.

As the selectables could use some more customisation, you could also make a feature request for jQuery UI developers to include it in the next official version.

Hello爱情风 2024-11-09 04:26:49

您必须在代码中注入两个简单的元素才能实现您想要的效果。每当您需要反转/切换功能时,这只会反转元密钥布尔值。

options: {
    appendTo: 'body',
    autoRefresh: true,
    distance: 0,
    filter: '*',
    tolerance: 'touch',
    inverted: false /* <= FIRST*/
},

_mouseStart: function(event) {
    var self = this;

    this.opos = [event.pageX, event.pageY];

    if (this.options.disabled)
        return;

    var options = this.options;

    if (options.inverted) /* <= SECOND*/
        event.metaKey = !event.metaKey; /* <= SECOND*/

You have to inject two simple elements in the code to achieve what you want. This just inverts the metaKey boolean, whenever you need inverted/toggle functionality.

options: {
    appendTo: 'body',
    autoRefresh: true,
    distance: 0,
    filter: '*',
    tolerance: 'touch',
    inverted: false /* <= FIRST*/
},

_mouseStart: function(event) {
    var self = this;

    this.opos = [event.pageX, event.pageY];

    if (this.options.disabled)
        return;

    var options = this.options;

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