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


当前回答

如果脚本标签有延迟属性,则显示错误

未定义Uncaught ReferenceError: $

并且必须从脚本标记中删除属性defer

其他回答

有一种方法可以让它在其余代码运行之前自动加载javascript。

进入Views\Shared_Layout.html并添加以下内容

<head>
  <*@ Omitted code*@>
  <script src="~/Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>
</head>

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.

我也有同样的问题,现在我已经解决了。我的环境如下所示;

Laravel与刀片页。我有一个主布局,并使用部分创建新的页面从我的主页面布局(模板)。

我从我的section页面加载JQuery,而不是我的主布局。我把它从section页面改为从主布局和头部标签的顶部加载JQuery。

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

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

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

如上所述,它的发生是由于$变量的冲突。

我通过为jQuery保留一个没有冲突的次要变量来解决这个问题。

var $j = jQuery.noConflict();

然后在任何地方使用它

$j( "div" ).hide();

更多细节可以在这里找到