我有一个简单的jquery点击事件

<script type="text/javascript">
    $(function() {
        $('#post').click(function() {
            alert("test"); 
        });
    });
</script>

以及在site.master中定义的jquery引用

<script src="<%=ResolveUrl("~/Scripts/jquery-1.3.2.js")%>" type="text/javascript"></script>

我已经检查了脚本被正确解析,我能够看到标记,并直接在firebug中查看脚本,所以我必须被发现。然而,我仍然得到:

$没有定义

jquery没有一个是有效的。我还尝试了它的各种变体,比如$(document)。ready和jQuery等。

这是一个。net 3.5上的MVC 2应用程序,我确信我真的很密集,谷歌上的所有地方都说要检查文件是否被正确引用,我已经检查了一遍又一遍,请建议!: /


当前回答

这意味着您的jQuery库还没有加载。

您可以在拖动jQuery库后移动代码。

或者你可以用这样的东西

window.onload = function(){
  // Your code here
  // $(".some-class").html("some html");
};  

其他回答

我也有同样的问题,没有案例能解决我的问题。唯一对我有用的,就是把它放在网站上。主文件,下一个:

<script src="<%= ResolveUrl("~/Scripts/jquery-1.7.1.min.js") %>" type="text/javascript"></script>
<script src="<%= ResolveUrl("~/Scripts/bootstrap/js/bootstrap.min.js") %>" type="text/javascript"></script>

With src="<%= ResolveUrl("")…内容页中jQuery的加载是正确的。

如果你把你的jquery.js文件放在html文件所在的同一个文件夹下,或者在一些子文件夹中,Firebug的问题就解决了。例如,如果你的html文件在C:/folder1/下,那么你的js文件应该在C:/folder1/(或C:/folder1/folder2等)下的某个地方,并在html文档中相应地定位。希望这能有所帮助。

jQuery似乎无法正常加载。您使用的是哪个源/版本?

或者,它可能是名称空间冲突,所以尝试显式使用jQuery而不是使用$。如果这可以工作,您可能喜欢使用noConflict来确保使用$的其他代码不会中断。

我在这里没有发现,但确实发生在我身上。请确保您没有包含jQuery精简版,因为该版本的jQuery库不包括Ajax功能。

结果是“$”可以工作,但是$。例如,Get将返回一个错误消息,说明该函数未定义。

解决方案:包含完整版的jQuery。

I had this problem once for no apparent reason. It was happenning locally whilst I was running through the aspnet development server. It had been working and I reverted everything to a state where it had previously been working and still it didn't work. I looked in the chrome debugger and the jquery-1.7.1.min.js had loaded without any problems. It was all very confusing. I still don't know what the problem was but closing the browser, closing the development server and then trying again sorted it out.