【问题标题】:Dialog doesn't stay child of parent对话框不会保留为父级的子级
【发布时间】:2015-04-02 01:56:17
【问题描述】:

如果我这样做:

$("#my_button").click(function(){
    $("<div />")
    .appendTo("#main_section")
});

生成的div#main_section 的子代。但是,当我这样做时:

$("#my_button").click(function(){
    $("<div />")
    .appendTo("#main_section")
    .dialog({
        "title" : "ABC"
        "width" : 400,
        "height" : 300,
        "modal" : false,
        "resizable" : true,
        "close" : function(){ $(this).remove(); },
        "Cancel" : function(){ $(this).dialog("close"); },
    })
     .draggable({ containment: "parent" });
});

生成的对话框最终成为body 的直接子级。

我尝试过使用和不使用:

appendTo : "#main_section"

对话框选项。

这是我更新的代码:

$("#my_button").click(function(){
    $("<div />")
        .dialog()
        .draggable({ containment: "#main_section" });
});

我的对话框仍然不包含在#main_section 中(也不是它的子对象)

【问题讨论】:

    标签: jquery jquery-ui dom jquery-ui-dialog


    【解决方案1】:

    jQuery UI 可拖动包含

    .draggable({ containment: "#main_section" });

    您需要为元素设置边界而不是父元素...

    • 现在您还可以在正确包含对话框后删除appendTo()

    限制拖动到指定元素或区域的范围内。

    支持多种类型:

    选择器:可拖动元素将包含在选择器找到的第一个元素的边界框内。如果没有元素 找到,不会设置收容措施。

    元素:可拖动元素将包含在该元素的边界框内。

    字符串:可能的值:“父”、“文档”、“窗口”。

    数组:定义边界框的数组,格式为 [ x1, y1, x2, y2 ]。

    【讨论】:

    • 我已经添加了我的代码更新,它仍然不包含在 #main_section 中(也不是它的孩子,但这是次要的)
    • 我已经用你提议的最简单的精简版再次更新了它。我就是无法让它工作。
    • $("&lt;div /&gt;")这是在选择什么?
    • 它没有选择任何东西,它正在创建一个新的空 div(然后将其设置为对话框)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 2018-02-22
    • 1970-01-01
    • 2021-12-02
    相关资源
    最近更新 更多