开发者

jQuery $ is not a function in Firefox, works in Chrome and works with $(document).ready()

I'm implementing jQuery in a site and am getting the "$ is not a function" in Firefox when I try to use a selector, 开发者_如何学Gobut $(document).ready() works perfectly right before it. My code looks like this

<script>
     $(document).ready(function(){
          alert("hi")
     }); // Works fine
     function showDiv(){
          $("#traditionalCC").hide();
     }
     //Throws error
</script>

Does anyone know why this happens, and why it works in Chrome and Firefox.


The key difference between your two examples (the working and non working ones) is that the first one is using the document ready event. That happens when the page is fully loaded. Not sure when you're other one is getting invoked, but my guess is that it is being called before your <script> tag include for jquery.js itself.


Try

<script> 
   $(function() {
      alert("hi") 
    }); // Works fine 
    function showDiv(){ 
      $("#traditionalCC").hide(); 
    } 
    //Throws error 
</script> 


Try to use

$(document).ready(function() {
      $ = jQuery.noConflict();
});

Fix your script declaration to <script type="text/javascript">

Verify if your script is after the jQuery lib include.

I hope it help.


I have found that sometimes Firefox gets "hosed" and I have to quit and relaunch.


In case anyone comes across this in the future, the problem was FireBug. I uninstalled and reinstalled and the issue went away.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜