我有一个简单的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应用程序,我确信我真的很密集,谷歌上的所有地方都说要检查文件是否被正确引用,我已经检查了一遍又一遍,请建议!: /


当前回答

我使用Url。知足而从不有问题。

<script src="<%= Url.Content ("~/Scripts/jquery-1.4.1.min.js") %>" type="text/javascript"></script>

其他回答

在这里尝试了所有方法都没有结果之后,我简单地通过将脚本src标记从主体移动到头部来解决了这个问题

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.

检查jquery文件的确切路径是否包含在内。 < script src = "资产/插件/ jquery / jquery.min.js " > < /脚本>

如果你在你的页面底部添加这个,请在这个声明下面调用JS函数。

使用此代码测试检查, <脚本type = " text / javascript”> / * * * *由dadennew创建 *使用ajax提交电子邮件订阅 *发送电子邮件地址 *发送控制器 *收到回复 * / $(document).ready(function(){//你可以用Jquery替换$ jquery working~!”); });

和平!

确保你真的加载了jquery 这不是jquery——这是ui!

  <script language="JavaScript" 
    src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js">
  </script>

这是一个正确的jquery脚本源代码:

 <script language="JavaScript"  src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>

如果jQuery插件调用在</body>旁边,并且你的脚本是在</body>之前加载的,你应该让你的代码在窗口后运行。Onload事件,像这样:

window.onload = function() {
  //YOUR JQUERY CODE
}

`

因此,您的代码将只在窗口加载后运行,此时所有资产都已加载。此时,将定义jQuery($)。

如果你使用它:

$(document).ready(function () {
  //YOUR JQUERY CODE
});

`

$现在还没有定义,因为它是在jQuery加载之前调用的,你的脚本在控制台的第一行就会失败。