【问题标题】:How would you use .not(this) in the case of the following code?在以下代码的情况下,您将如何使用 .not(this) ?
【发布时间】:2011-10-18 22:05:18
【问题描述】:

如果是这段代码,你会怎么说:not(this):

$(".draghandle").droppable({
    accept: ".draghandle:not(this)"
    drop: function ( event, ui ) {
        $( this )
        .addClass( "ui-state-highlight" )
        .find( "p" )
        .html( "Dropped!" );
    }
});

您会看到接受选项旁边的选择器可能无效,虽然我没有检查过,但我想知道您是否有解决方案来验证它?我还没有找到一个,但如果你有一个解决方案也改变了代码的另一部分,请不要灰心,我只需要一个修复。 这是我正在尝试扩展的类似问题:jQuery: exclude $(this) from selector 这是我用作参考的主要代码的链接:http://jqueryui.com/demos/droppable/#accepted-elements

【问题讨论】:

    标签: jquery jquery-ui jquery-selectors properties optional-parameters


    【解决方案1】:

    说明

    您可以传递一个 jQuery 列表作为接受参数,而不仅仅是一个选择器。在下面的演示中,我将所有.ui-widget-content 元素都设为可丢弃除了 #draggable 之一。 (颠倒原始的 jQuery UI 站点演示)。我传递给accept 的参数是$(".ui-widget-content").not("#draggable")


    演示

    http://jsfiddle.net/5TZ7b/
    http://jsfiddle.net/5TZ7b/show


    代码

    $(function() {
        $( "#draggable, #draggable-nonvalid" ).draggable();
        $( "#droppable" ).droppable({
            accept: $(".ui-widget-content").not("#draggable"),
            activeClass: "ui-state-hover",
            hoverClass: "ui-state-active",
            drop: function( event, ui ) {
                $( this )
                    .addClass( "ui-state-highlight" )
                    .find( "p" )
                    .html( "Dropped!" );
            }
        });
    });
    

    结论

    您可以使用以下作为传递给accept的参数。

    $(".draghandle").not(this)
    

    【讨论】:

    • 感谢您的帮助,我不知道您可以将其作为接受参数传递。 :)
    猜你喜欢
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    相关资源
    最近更新 更多