我有一个简单的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保留一个没有冲突的次要变量来解决这个问题。

var $j = jQuery.noConflict();

然后在任何地方使用它

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

更多细节可以在这里找到

其他回答

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

我们有同样的问题....但是我不小心检查了文件夹属性并设置了一些东西…

你必须检查你正在访问的每个文件夹的属性。

右键单击文件夹 “权限”选项卡 设置文件夹访问权限: OWNER:创建和删除文件 GROUP:访问文件 其他:访问文件

我希望这是解决方案......

首先,您需要确保加载了jQuery脚本。这可能来自CDN或本地网站。如果你在尝试使用jQuery之前不先加载这个,它会告诉你jQuery没有定义。

<script src="jquery.min.js"></script>

这可能是在HEAD或页脚,只是确保你加载它之前,你试图调用任何其他jQuery的东西。

然后,您需要使用以下两种解决方案之一

(function($){
// your standard jquery code goes here with $ prefix
// best used inside a page with inline code, 
// or outside the document ready, enter code here
 })(jQuery); 

or

jQuery(document).ready(function($){
// standard on load code goes here with $ prefix
// note: the $ is setup inside the anonymous function of the ready command
});

请注意,很多时候$(document).ready(function(){//code here});不会起作用。

我也遇到过同样的问题,通过以下步骤解决。 脚本的顺序应该如下所述

    <script src="~/Scripts/jquery-3.3.1.min.js"></script>
    <script src="~/Scripts/jquery-ui.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>

这个序列对我的代码不正确,我按照上面的方法纠正了这一点,它解决了我的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.