$ is not a function - jQuery error

前端 未结 10 1120
余生分开走
余生分开走 2020-11-29 20:41

I have the jQuery loaded fine, I\'ve quadruple-checked, though I\'m getting this error in FireBug \"$ is not a function\" and my code doesn\'t work.

Here\'s my code:

相关标签:
10条回答
  • 2020-11-29 20:55

    There are two possible reasons for that error:

    1. your jquery script file referencing is not valid
    2. try to put your jquery code in document.ready, like this:

      $(document).ready(function(){

      ....your code....

      });

    cheers

    0 讨论(0)
  • 2020-11-29 20:58

    When jQuery is not present you get $ is undefined and not your message.

    Did you check if you don't have a variable called $ somewhere before your code?
    Inspect the value of $ in firebug to see what it is.

    Slightly out of the question, but I can't resist to write a shorter code to your class assignment:

        var i = 1;
        $("ol li").each(function(){
            $(this).addClass('olli' + i++);
        });
    
    0 讨论(0)
  • 2020-11-29 21:09

    In Wordpress jQuery.noConflict() is called on the jQuery file it includes (scroll to the bottom of the file it's including for jQuery to see this), which means $ doesn't work, but jQuery does, so your code should look like this:

    <script type="text/javascript">
      jQuery(function($) {
        for(var i=0; i <= 20; i++) 
          $("ol li:nth-child(" + i + ")").addClass('olli' + i);
      });
    </script>
    
    0 讨论(0)
  • 2020-11-29 21:09

    It's really hard to tell, but one of the 9001 ads on the page may be clobbering the $ object.

    jQuery provides the global jQuery object (which is present on your page). You can do the following to "get" $ back:

    jQuery(document).ready(function ($) {
        // Your code here
    });
    

    If you think you're having jQuery problems, please use the debug (non-production) versions of the library.

    Also, it's probably not best to be editing a live site like that ...

    0 讨论(0)
提交回复
热议问题