设置jQuery UI对话框相对于打开它的元素的位置

use*_*789 4 jquery jquery-ui jquery-ui-dialog

我正在尝试将jQueryUI对话框放在被单击的元素上方以触发其打开.

我尝试了以下,但它无法正常工作.

$(function() {
    dialog = $( "#gridDialog" ).dialog({
    autoOpen: false,
    modal: true,
    buttons: {
        "Close": function(event, ui) {
            dialog.dialog( "close" );
         }
    },
    open: function(event,ui){
        dialog.dialog( "option", "position", {at: "left top", of: event } );
    }
  });           
});
Run Code Online (Sandbox Code Playgroud)

T J*_*T J 6

你的方法的问题是,你想它自己内部的对话框定位open()方法,它接收一个自定义的jQuery UI的事件对象,不具有pageXpageY它是由jQuery UI的预期性能的position()方法.

相反,如果你设置的对话框里面的位置click打开它之前的事件处理程序,你可以简单地通过this,或点击事件对象的值position()期权属性.

例如:

 $("#dialog").dialog({
   autoOpen: false
 });
 $(".box").click(function() {
   $("#dialog").dialog("option", "position", {
     at: "left top",
     of: this // this refers to the cliked element
   }).dialog("open");
 });
Run Code Online (Sandbox Code Playgroud)
.box {
  width: 100px;
  height: 100px;
  background: dodgerblue;
}
#left {
  float: left;
}
#right {
  float: right;
}
Run Code Online (Sandbox Code Playgroud)
<link href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<div id="left" class="box"></div>
<div id="right" class="box"></div>
<div id="dialog" title="Basic dialog">
  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>
Run Code Online (Sandbox Code Playgroud)