我有根据数据库中的内容动态生成的链接。使用动态链接打开jQuery UI对话框
的联系最终看起来像
<ul>
<li><a href="/Updates/LoadArticle?NewsId=3" id="article">Article 3</a></li>
<li><a href="/Updates/LoadArticle?NewsId=2" id="article">Article 2</a></li>
<li><a href="/Updates/LoadArticle?NewsId=1" id="article">Article 1</a></li>
</ul>
我拼凑起来的脚本是
$(document).ready(function() {
$("#article").click(function (e) {
InitializeDialog($("#news"), $(this).attr("href"));
e.preventDefault();
$("#news").dialog("open");
});
//Method to Initialize the DialogBox
function InitializeDialog($element, page) {
$element.dialog({
autoOpen: true,
width: 400,
resizable: false,
draggable: true,
title: "Update",
modal: true,
show: 'fold',
closeText: 'x',
dialogClass: 'alert',
closeOnEscape: true,
position: "center",
open: function (event, ui) {
$element.load(page);
},
close: function() {
$(this).dialog('close');
}
});
}
});
这适用于在列表中的第一篇文章 - 打开的对话框中,但orther文章打开一个单独的页面。我假设这是因为ID不是唯一的。
我的问题更多的是如何为任何id(比如article1,article2等)创建一个通用的jQuery函数。
我已经在jQuery上进行了大约20分钟的培训,所以我在黑暗中拍摄了什么地方。
谢谢。
标识*真的*应该是独一无二的DOM。 – techfoobar 2012-08-15 15:45:54
@Erik:您可能对我的jQuery插件dialogWrapper感兴趣:https://bitbucket.org/MostThingsWeb/dialogwrapper/wiki/Home它简化了动态创建对话框。 – 2012-08-15 15:46:31
理想情况下,您应该为每个链接使用class =“article”,并使用$('。article')进行事件绑定。 – techfoobar 2012-08-15 15:46:47