I want to make a popup box (with text in it) come out when i click a hyperlink. I have 5 hyperlinks in my html. Here is the code :
当我点击一个超链接时,我想让一个弹出框(里面有文字)出来。我的html中有5个超链接。这是代码:
<div class="four columns">
<h4>
<a id="OpenDialog" href="#" >Open dialog 1</a>
</h4>
<img src="one.jpg" />
<div id="dialog" title="Dialog Title 1">dialog text 1</div>
</div>
<div class="four columns">
<h4>
<a id="OpenDialog" href="#" >Open dialog 2</a>
</h4>
<img src="two.jpg" />
<div id="dialog" title="Dialog Title 2">dialog text 2</div>
</div>
I put this in my html as well :
我把它放在我的html中:
<script type="text/javascript">
$(document).ready(function () {
$("#OpenDialog").click(function () {
$("#dialog").dialog({modal: true, height: 590, width: 1005 });
});
});
</script>
and I also included this ready scripts :
我还包括这个准备好的脚本:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
But the thing is that the pop up functions only in the first hyperlink.
但问题是弹出功能只在第一个超链接中起作用。
2 个解决方案
#1
4
I would use jquery parent and children to get what you want. (jsfiddle: http://jsfiddle.net/pjVcR/2/)
我会使用jquery父母和孩子来获得你想要的东西。 (jsfiddle:http://jsfiddle.net/pjVcR/2/)
<script>
$("a").click(function(event) {
$(this).parent().parent().children(".dialog").dialog({
close: function( e, ui ) {
$(this).dialog('destroy');
}
});
});
</script>
In this case you would have to hide the .dialog divs in the beginning. Moreover, change the dialog container to have a class (and not an id) named "dialog". This way you will not have many divs with the same id, and your functionality will be there.
在这种情况下,您必须在开头隐藏.dialog div。此外,将对话框容器更改为具有名为“dialog”的类(而不是id)。这样你就不会有很多具有相同id的div,你的功能就在那里。
Here some references:
这里有一些参考:
- http://api.jquery.com/parent/
- http://api.jquery.com/parent/
- http://api.jquery.com/children/
- http://api.jquery.com/children/
- Getting the ID of the element that fired an event
- 获取触发事件的元素的ID
#2
0
first add these files
首先添加这些文件
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
then
然后
<script type="text/javascript">
$(document).ready(function () {
$("#OpenDialog").click(function (e) {
e.preventDefault();
$("#dialog").dialog({modal: true, height: 590, width: 1005 });
});
});
</script>
reference jQuery dialog box
引用jQuery对话框
#1
4
I would use jquery parent and children to get what you want. (jsfiddle: http://jsfiddle.net/pjVcR/2/)
我会使用jquery父母和孩子来获得你想要的东西。 (jsfiddle:http://jsfiddle.net/pjVcR/2/)
<script>
$("a").click(function(event) {
$(this).parent().parent().children(".dialog").dialog({
close: function( e, ui ) {
$(this).dialog('destroy');
}
});
});
</script>
In this case you would have to hide the .dialog divs in the beginning. Moreover, change the dialog container to have a class (and not an id) named "dialog". This way you will not have many divs with the same id, and your functionality will be there.
在这种情况下,您必须在开头隐藏.dialog div。此外,将对话框容器更改为具有名为“dialog”的类(而不是id)。这样你就不会有很多具有相同id的div,你的功能就在那里。
Here some references:
这里有一些参考:
- http://api.jquery.com/parent/
- http://api.jquery.com/parent/
- http://api.jquery.com/children/
- http://api.jquery.com/children/
- Getting the ID of the element that fired an event
- 获取触发事件的元素的ID
#2
0
first add these files
首先添加这些文件
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
then
然后
<script type="text/javascript">
$(document).ready(function () {
$("#OpenDialog").click(function (e) {
e.preventDefault();
$("#dialog").dialog({modal: true, height: 590, width: 1005 });
});
});
</script>
reference jQuery dialog box
引用jQuery对话框