使用 jQuery .load 会破坏与其他库资源的链接吗?
我有一个带有 div 的简单 html 页面。 我正在使用 jQuery 将 aspx 应用程序的内容加载到“content”div 中。 代码如下所示:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js">
</script>
<script type="text/javascript">
jQuery.noConflict();
</script>
</head>
<body>
<div id="content">
<div id="loading">
<div class="loading-indicator">Loading...</div>
</div>
</div>
</body>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#content").load("default.aspx");
});
</script>
</html>
问题是default.aspx 使用shadowbox 和其他javascript 库。 当该代码尝试在 default.aspx 上执行时,它的行为就像 js 源文件未加载一样。 我检查了 firebug 并且 js 文件在那里(没有 404 或其他任何东西)。 有人知道我错过了什么吗? 正如你所看到的,我使用了 jQuery noConflict 函数,因为我认为 $ 的使用可能与其他库冲突,但没有帮助......
I have a simple html page with a div. I am using jQuery to load the contents of an aspx app into the "content" div. Code looks like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js">
</script>
<script type="text/javascript">
jQuery.noConflict();
</script>
</head>
<body>
<div id="content">
<div id="loading">
<div class="loading-indicator">Loading...</div>
</div>
</div>
</body>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#content").load("default.aspx");
});
</script>
</html>
The problem is default.aspx uses shadowbox and other javascript libraries. When that code tries to execute on default.aspx it acts like the js source files were not loaded. I check in firebug and the js files are there (no 404 or anything). Anyone know what I'm missing? As you can see I used the jQuery noConflict function because I thought the use of $ might be conflicting with the other libraries but no help there...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有这样做的代码,它可能比需要的更详细,但嵌套的 js 文件应该不是问题。
I have code doing this, it might be more verbose than is needed, but nested js files shouldn't be a problem.
未执行的代码是否以脚本块的形式呈现,我了解加载的库,但任何脚本块或内联 JavaScript 在动态加载时都不会执行。 您必须想出一个解决方案来评估返回的脚本块是否有效。 我会看看是否可以从原型中挖掘一个示例,我记得他们有一个。
更新:
这直接来自原型...
当然,您可以根据需要进行简化,但是当动态返回页面时,您必须手动评估所有脚本,因为浏览器不会自动评估注入到元素中的脚本。
Is the code that is not executing rendered out as script blocks, I understand the libraries loaded but any script blocks or inline javascript will not execute when loaded dynamically like that. You have to come up witha solution that will evaluate the script blocks returned for any of it to be valid. I'll see if I can dig an example from prototype, I remember them having one.
UPDATE:
This is straight from prototype...
Of course you can simplify that for your needs, but when a page is returned dynamically you have to manually evaluate all scripts as the browser will not evaluate script injected into the element automagically.
似乎是一个常见问题: http ://andreineculau.wordpress.com/2006/09/29/ajax-ondemand-javascript-or-dynamic-script-tags/
我猜它是浏览器内置的安全性,以防止ajax响应运行任意脚本。
Seems like a common problem: http://andreineculau.wordpress.com/2006/09/29/ajax-ondemand-javascript-or-dynamic-script-tags/
I'm guessing it's security built into browsers to prevent an ajax response from running arbitrary script.