且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jquery ui 对话框使用按钮和内容 div 上的相同类打开多个对话框

更新时间:2023-02-11 14:34:33

您实际上需要一种不同的方法,一种非直观的方法,如下所示:

You actually need a different approach here, a non-intuitive one, like this:

jQuery(function($) {
  $('.helpButton').each(function() {  
    $.data(this, 'dialog', 
      $(this).next('.helpDialog').dialog({
        autoOpen: false,  
        modal: true,  
        title: 'Info',  
        width: 600,  
        height: 400,  
        position: [200,0],  
        draggable: false  
      })
    );  
  }).click(function() {  
      $.data(this, 'dialog').dialog('open');  
      return false;  
  });  
});  

你可以在这里测试一下.

为什么必须这样做?因为 .dialog() 将其包装在对话框元素中的内容移动到 <body> 的末尾,因此 .next() 将不再找到它...通过使用 jQuery.data() 我们正在维护对我们要打开的对话框的引用.

Why must you do this? because .dialog() moves the content it wraps in dialog elements to the end of the <body>, so .next() will no longer find it...by using jQuery.data() we're maintaining a reference to the dialog we want to open.