使用 JQuery 和 BlogEngine.net 期望对象
我有以下代码:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
});
页面加载失败,并显示“预期对象”。如果我将鼠标悬停在 Visual Studio 中的 $(document) 上,它会展开以显示其属性。这里没有其他对象,所以除非它在 jquery 库中失败,否则还有什么可能导致此问题?
BlogEngine 脚本也不应该发生任何冲突。我在 blog.js 中将所有 $ 变量重命名为 $BE,因此 JQuery 可以单独使用 $。
I have the following code:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
});
The page fails on load with "Object expected". If I hover over $(document) in Visual Studio, it expands to show its properties. There is no other object here, so unless it's failing within the jquery library, what else could be causing this?
There shouldn't be any collisions happening with the BlogEngine script either. I renamed all of the $ variables to $BE in blog.js, so JQuery gets sole use of $.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
失败的最可能原因是
$(document).ready()
出现在 jQuery 脚本包含标记之前。失败的方式或原因取决于您如何包含文件,但解决此问题的一种方法是在
Utils
静态类中使用 BlogEngine.NET 的AddJavaScriptInclude()
并在BlogBasePage
类的OnLoad
方法中添加 jQuery。例如,
Now jQuery 将包含在
中。如果您的脚本位于母版页中,那么我建议将其放入
中,以确保 jQuery 脚本包含位于您的代码之前,并避免出现与以前相同的情况;如果您的脚本位于另一个外部文件中,那么您也可以使用
AddJavaScriptInclude()
将其包含在页面中,只需确保它是在添加 jQuery 文件之后出现的。另一件需要注意的事情是,像您所做的那样重命名 Blog.js 文件和 BlogEngine.NET 附带的任何其他 js 文件中的变量
$
可能是明智的,或者使用 jQuery 的$.noConflict();
然后将 jQuery 代码包装在一个自调用匿名函数中,这样您仍然可以在函数内部使用 jQuery 的
$
简写。The most probable reason as to why it fails is that
$(document).ready()
appears before the jQuery script include tag.How or why it fails will depend upon how you're including the file, but one way to solve this is to use BlogEngine.NET's
AddJavaScriptInclude()
in theUtils
static class and add jQuery in theBlogBasePage
class'OnLoad
method.For example,
Now jQuery will be included in the
<head>
. If you have your script in the master page, then I would advise putting it into the<body>
to ensure that the jQuery script include will come before your code and avoid the same situation as before; if your script is in another external file, then you could useAddJavaScriptInclude()
to include it in the page too, just make sure that it comes after adding the jQuery file.One other thing to note, it may be sensible to either rename the variable
$
in the Blog.js file and any other js files that BlogEngine.NET comes with as you have done, or use jQuery's$.noConflict();
and then wrap your jQuery code inside a self-invoking anonymous function like soso that you can still use the
$
shorthand for jQuery inside of the function.我不知道为什么你的例子失败了,但我很确定你可以通过执行以下操作来获得 document.ready 行为:
I don't know why your example fails, but I'm pretty sure you can get the document.ready behvaiour just by doing: