匿名函数错误:未定义 $


Anonymous Function Error: $ is not defined

当我的PHP页面使用javascript文件(my_scripts.js)中的内容时,Google Developer Tools显示以下错误:

"未捕获的引用错误:$ 未定义脚本.js:1(匿名函数)"

my_scripts.js内容

$('a.button').click(function(){ 
    window.location.href = "another_page.php";         
});

页面和脚本根据需要工作。单击元素链接到请求的页面,但错误仍然存在。

1) 导致错误的原因是什么?2)可以忽略它还是应该忽略它?

1)看起来你的问题是jQuery还没有被加载。

确保在scripts.js之前加载 jQuery。

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head> 

2)永远不应该忽视错误。

你需要在之前加载jquery库!请下载jquery.min.js http://www.jquery.com/download/另外,像这样写:

$(doucment).ready(function(){
    $('a.button').on('click',function(){ 
        window.location.href = "another_page.php";         
    });
});

如果你已经在你的文件中包含了jQuery,但它仍然不起作用,你应该尝试,

jQuery(doucment).ready(function(){
   jQuery('a.button').on('click',function(){ 
      window.location.href = "another_page.php";         
   });
});

这是由于可能存在一些冲突的js文件而导致的。