“ $ is undefined”错误,但“ jQuery”有效

时间:2018-09-12 11:46:32

标签: jquery

我有这些代码行,这是错误消息的原因。在我看来,代码中没有错误,但是我想我错了:

jQuery(document).ready(function() {
  console.log("ready!");

  $("#hover-table").hover(function() { // THIS IS LINE 164    
    $('#table-wrapper').show();
  }, function() {
    $('#table-wrapper').hide();
  });
}); 

我在Chrome控制台中收到此错误消息。 console.log的打印没有问题。

  

(索引):164未捕获的TypeError:$不是函数
   在HTMLDocument。 ((index):164)
   在我(jquery.js?ver = 1.12.4:2)
   at Object.fireWith [as resolveWith](jquery.js?ver = 1.12.4:2)
   在Function.ready(jquery.js?ver = 1.12.4:2)
   在HTMLDocument.K(jquery.js?ver = 1.12.4:2)

我在这里想念什么?

2 个答案:

答案 0 :(得分:5)

另一个使用美元符号而不是jQuery的库可能会造成这种冲突,因此您可以将所有美元符号jQuery替换为<script> jQuery(document).ready(function() { console.log("ready!"); jQuery("#hover-table").hover(function() { // THIS IS THE LINE 164--- jQuery('#table-wrapper').show(); }, function() { jQuery('#table-wrapper').hide(); }); }); </script> ,如:

jQuery(document).ready(function($){
    //You can now use $ as your jQuery object.
});

或者您也可以将其定义为供内部块使用,例如:

  dQt/dt = (g*At/Lt) * (yt - c*Qt*np.abs(Qt)/At**2) #(1)

  dy/dt = -Qt/As #(2)

答案 1 :(得分:2)

尝试下面的代码,看起来您已经将$更改为jQuery

<script> 
jQuery(document).ready(function(){
   console.log( "ready!" );
   jQuery("#hover-table").hover(function(){ // THIS IS THE LINE 164---
     jQuery('#table-wrapper').show();
   },function(){
     jQuery('#table-wrapper').hide();
  });
 }); 
</script>