带有动态内容的 jQuery 对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3423842/
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
jQuery dialog with dynamic content
提问by DEH
I want to create a jQuery dialog on-the-fly. I'm using this:
我想即时创建一个 jQuery 对话框。我正在使用这个:
var newDiv = $(document.createElement('div'));
$(newDiv).html('hello there');
$(newDiv).dialog();
I then have this in the html header:
然后我在 html 标题中有这个:
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.1.custom.min.js"></script>
When I try running the JS in IE7 I get the following error on the $(newDiv).dialog(); line : Object doesn't support this property or method.
当我尝试在 IE7 中运行 JS 时,我在 $(newDiv).dialog(); 上收到以下错误;行:对象不支持此属性或方法。
Anyone know what's going on?
有谁知道这是怎么回事?
回答by Nick Craver
Your code works, you can test it here, that means you probably have a script include problem, make sure that your files are under a js
folder beside the page, or if you intended them to be from site root, use /js
instead.
您的代码有效,您可以在此处进行测试,这意味着您可能有脚本包含问题,请确保您的js
文件位于页面旁边的文件夹下,或者如果您希望它们来自站点根目录,请/js
改用。
Or, consider using a CDN.
或者,考虑使用 CDN。
You can make your code a bit more efficient (I realize it's just a test), like this:
你可以让你的代码更有效率(我意识到这只是一个测试),像这样:
var newDiv = $(document.createElement('div'));
newDiv.html('hello there');
newDiv.dialog();
This works because newDiv
is alreadya jQuery element, no reason to clone the object each time...or a bit shorter:
这是有效的,因为newDiv
它已经是一个 jQuery 元素,没有理由每次都克隆对象……或者更短一点:
$('<div />').html('hello there').dialog();
回答by Chad Kuehn
Here is an alternative way of creating dialogs and their messages dynamically:
这是动态创建对话框及其消息的另一种方法:
$('<div></div>').dialog({
modal: true,
title: "Confirmation",
open: function() {
var markup = 'Hello World';
$(this).html(markup);
},
buttons: {
Ok: function() {
$( this ).dialog( "close" );
}
}
}); //end confirm dialog
See it in action: http://jsfiddle.net/DYbwb/
看看它在行动:http: //jsfiddle.net/DYbwb/
回答by Leon
The code is good, what you need is a reference of jquery and jquery ui
代码不错,你需要的是jquery和jquery ui的参考
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>