当您单击 <a href> 链接时如何启动 jquery 对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1328566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do you launch a jquery dialog when you click on a <a href> link
提问by leora
how do you launch a jquery dialog when you click on a link
单击链接时如何启动 jquery 对话框
this doesn't seem to work
这似乎不起作用
<script type="text/javascript">
$(document).ready(function() {
$("a.To").click(function(e) {
e.preventDefault();
$("#dialog").dialog({height:300});
});
in the body:
在身体里:
<a href="#" id="To">To</a>
回答by Daniel Moura
For id you should use # :
对于 id 你应该使用 # :
$("a#To")
Dot is for classes.
点用于类。
回答by typeoneerror
I did this recently for confirming delete links in my cms. First you should instantiate a dialog window (this is so if you click close on the dialog and then open it again, it shows up...otherwise, it's destroyed):
我最近这样做是为了确认我的 cms 中的删除链接。首先你应该实例化一个对话框窗口(如果你在对话框上点击关闭然后再次打开它,它就会显示出来......否则,它会被破坏):
$(document).ready(function()
{
/**
* Create a dialog box for confirming deletes.
* This creates the box at domready. The box is opened
* by a call to dialog("open") in the delete link.
*/
$("#delete-dialog").dialog({
autoOpen : false,
bgiframe : true,
buttons : {
"Yes, I'm sure" : function()
{
$(this).dialog("close");
var href = $(this).dialog("option", "href");
var row = $(this).dialog("option", "row");
$.doDelete(href, row);
},
"Cancel" : function()
{
$(this).dialog("close");
}
},
height : 150,
modal : true,
overlay : {
backgroundColor : "#000000",
opacity : 0.75
},
resizable : false
});
});
Then "hook up" the a tags (still in the document.ready block):
然后“连接” a 标签(仍在 document.ready 块中):
/**
* Make all delete links confirm before sending to delete path.
*/
$("a.delete-href").live("click", function(event)
{
event.preventDefault();
var href = $(this).attr("href");
var row = $(this).parent().parent();
// pass some custom options to the dialog
$("#delete-dialog").dialog("option", "href", href);
$("#delete-dialog").dialog("option", "row", row);
// open the previously init'ed dialog
$("#delete-dialog").dialog("open");
});
回答by redsquare
Your using a class selector but your looking for the id...you need the following.
您使用的是类选择器,但您正在寻找 id...您需要以下内容。
$("#To").click(function(e) {
e.preventDefault();
$("#dialog").dialog({height:300});
});
回答by Alex Gyoshev
since you're selecting by the id
attribute, the proper selector is "a#To"
rather than "a.To"
由于您是按id
属性选择的,因此正确的选择器"a#To"
不是"a.To"
回答by mkoryak
the code:
编码:
$("#dialog").dialog({height:300});
will build the dialog, the code to open a dialog is:
将构建对话框,打开对话框的代码是:
$("#dialog").dialog("open");
edit:although, i think autoOpen is set to true by default so your code should work unless your selector is broken. Id recommend setting autoopen to false and using the open param to open the dialog (that way you dont have to rebuild it whenever you try to open it):
编辑:虽然,我认为 autoOpen 默认设置为 true,因此除非您的选择器损坏,否则您的代码应该可以工作。我建议将 autoopen 设置为 false 并使用 open 参数打开对话框(这样您就不必在尝试打开它时重建它):
$("#dialog").dialog({height:300, autoOpen:false});