我有允许用户打开几个对话框的软件。在不深入解释我正在做什么的情况下,是否可以从对话框中提取对话框的ID?例如,我可能有一个名为“dialog1”的对话框,而另一个名为“dialog5”的对话框,如果我单击这些对话框上的HTML中的一个链接(而不是单击对话框按钮),我需要知道它的'dialog1‘还是'dialog5’。
目前,我看到了两个选项: 1#打开对话框中包含对话框ID的GET变量,然后让PHP在我需要的地方回显其ID (并以任何ajax形式重新发送该信息,如果对话框刷新,则让该页面返回数据等等等).
2#有一个带有特定id的HTML元素的每个页面,然后让JavaScript在加载时查找该元素,用一个随机生成的元素替换它(这样就不会与新的对话框发生冲突),然后使用随机生成的元素遍历dom来动态地提取ID.
我尝试了这两种方法,而且方法2似乎更好,因为它所需的维护要少得多,但它是一个丑陋的解决方案(如果DOM发生变化,它就会崩溃)。有什么更好的方法来完成这个任务吗?
发布于 2015-03-31 04:58:29
将委托的单击事件绑定到所有对话框(“公共类”)的所有对话框ids exists.use公共类的外部包装器上。
<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");
})https://stackoverflow.com/questions/29360800
复制相似问题