且构网

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

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

更新时间:2023-02-11 14:39:18

你实际上需要一种不同的方法,直观的,像这样:

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.