I have software that allows users to open several dialog boxes. Without going into a very long explanation of what I am doing, is it possible to pull the ID of the dialog box from within the dialog? So for example I might have a dialog box called 'dialog1' then another called 'dialog5', if I click on a link in the HTML of either on these dialog boxes (not clicking on the dialog box buttons), I need to know if its 'dialog1' or 'dialog5'.
我有软件允许用户打开几个对话框。如果不对我正在做的事情进行很长的解释,是否可以从对话框中提取对话框的ID?所以例如我可能有一个名为'dialog1'的对话框,然后另一个名为'dialog5',如果我点击这些对话框上的HTML中的链接(不点击对话框按钮),我需要知道是否它的'dialog1'或'dialog5'。
As it stands I see two options: 1# Open the dialog box with a GET variable that holds the ID of the dialog box, then have PHP echo out its ID in places I need (and re-send that information in any ajax forms, then have that page return the data if the dialog is refresh, etc, etc, etc)....
目前我看到两个选项:1#打开带有保存对话框ID的GET变量的对话框,然后让PHP在我需要的地方回显出它的ID(并在任何ajax表单中重新发送该信息,然后让该页面返回数据,如果对话框是刷新等等,等等)....
2# Have each page with an HTML dom element with a specific id, then have JavaScript on load find that element, replace it with a randomly generated element(that way there wont be a conflict with new dialog boxes), then use the randomly generated element to traverse the DOM to dynamically pull the ID...
2#让每个页面都带有一个带有特定id的HTML dom元素,然后在加载JavaScript时找到该元素,用随机生成的元素替换它(这样就不会与新的对话框发生冲突),然后使用随机生成的遍历DOM以动态提取ID的元素...
I tried both methods, and it seems method 2 is better, as it requires a lot less maintenance, but its an ugly solution (and would break if the DOM changes). Any better way to get this done?
我尝试了两种方法,似乎方法2更好,因为它需要更少的维护,但它是一个丑陋的解决方案(并且如果DOM改变会破坏)。有没有更好的方法来完成这项工作?
1 个解决方案
#1
0
Bind an delegated click event to the outer wrapper in which all dialog ids exists.use common class for all dialogs("common-class").
将委派的单击事件绑定到所有对话框ID都存在的外部包装器。使用所有对话框的公共类(“common-class”)。
<div class="outer-wrapper">
<div id="dialog-1" class="common-class" 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>
<div id="dialog-2" class="common-class" 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>
</div>
jQuery(".outer-wrapper").on("click",".common-class",function(){
var dialogclick = jQuery(this).attr("id");
})
#1
0
Bind an delegated click event to the outer wrapper in which all dialog ids exists.use common class for all dialogs("common-class").
将委派的单击事件绑定到所有对话框ID都存在的外部包装器。使用所有对话框的公共类(“common-class”)。
<div class="outer-wrapper">
<div id="dialog-1" class="common-class" 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>
<div id="dialog-2" class="common-class" 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>
</div>
jQuery(".outer-wrapper").on("click",".common-class",function(){
var dialogclick = jQuery(this).attr("id");
})