具有动态内容的jQuery对话框 [英] jQuery dialog with dynamic content
问题描述
我想即时创建一个jQuery对话框.我正在使用这个:
var newDiv = $(document.createElement('div'));
$(newDiv).html('hello there');
$(newDiv).dialog();
然后我在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>
当我尝试在IE7中运行JS时,在$(newDiv).dialog()上出现以下错误:线 : 对象不支持此属性或方法.
有人知道怎么回事吗?
您的代码有效,您可以在此处进行测试,这意味着您可能存在脚本包含问题,请确保您的文件位于页面旁边的js
文件夹 下,或者如果您希望这些文件来自站点根目录,请使用/js
代替.
或者,考虑使用CDN .>
您可以使代码更加高效(我意识到这只是一个测试),如下所示:
var newDiv = $(document.createElement('div'));
newDiv.html('hello there');
newDiv.dialog();
之所以可行,是因为newDiv
已经是 一个jQuery元素,没有理由每次都克隆对象了……或更短一些:
$('<div />').html('hello there').dialog();
I want to create a jQuery dialog on-the-fly. I'm using this:
var newDiv = $(document.createElement('div'));
$(newDiv).html('hello there');
$(newDiv).dialog();
I then have this in the html header:
<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.
Anyone know what's going on?
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.
Or, consider using a 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 already a jQuery element, no reason to clone the object each time...or a bit shorter:
$('<div />').html('hello there').dialog();
这篇关于具有动态内容的jQuery对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!