$ is not a function - jQuery error

前端 未结 10 1119
余生分开走
余生分开走 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:50

    That error kicks in when you have forgot to include the jQuery library in your page or there is conflict between libraries - for example you be using any other javascript library on your page.

    Take a look at this for more info:

    • Using jQuery with other libraries
    0 讨论(0)
  • 2020-11-29 20:51

    As RPM1984 refers to, this is mostly likely caused by the fact that your script is loading before jQuery is loaded.

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

    There are quite lots of answer based on situation.

    1) Try to replace '$' with "jQuery"

    2) Check that code you are executed are always below the main jquery script.

    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script type="text/javascript">
    jQuery(document).ready(function(){
    
    });
    </script>
    

    3) Pass $ into the function and add "jQuery" as a main function like below.

    <script type="text/javascript">
    jQuery(document).ready(function($){
    
    });
    </script>
    
    0 讨论(0)
  • 2020-11-29 20:53

    In my case I was using jquery on my typescript file:

    import * as $ from "jquery";
    

    But this line gives me back an Object $ and it does not allow to use as a function (I can not use $('my-selector')). It solves my problem this lines, I hope it could help anyone else:

    import * as JQuery from "jquery";
    const $ = JQuery.default;
    
    0 讨论(0)
  • 2020-11-29 20:54

    Use jQuery for $. I tried and work.

    0 讨论(0)
  • 2020-11-29 20:55
    <script type="text/javascript">
        $("ol li:nth-child(1)").addClass('olli1');
        $("ol li:nth-child(2)").addClass("olli2");
        $("ol li:nth-child(3)").addClass("olli3");
        $("ol li:nth-child(4)").addClass("olli4");
        $("ol li:nth-child(5)").addClass("olli5");
        $("ol li:nth-child(6)").addClass("olli6");
        $("ol li:nth-child(7)").addClass("olli7");
        $("ol li:nth-child(8)").addClass("olli8");
        $("ol li:nth-child(9)").addClass("olli9");
        $("ol li:nth-child(10)").addClass("olli10");
        $("ol li:nth-child(11)").addClass("olli11");
        $("ol li:nth-child(12)").addClass("olli12");
        $("ol li:nth-child(13)").addClass("olli13");
        $("ol li:nth-child(14)").addClass("olli14");
        $("ol li:nth-child(15)").addClass("olli15");
        $("ol li:nth-child(16)").addClass("olli16");
        $("ol li:nth-child(17)").addClass("olli17");
        $("ol li:nth-child(18)").addClass("olli18");
        $("ol li:nth-child(19)").addClass("olli19");
        $("ol li:nth-child(20)").addClass("olli20"); 
    </script>
    

    change this to

        <script type="text/javascript">
            jQuery(document).ready(function ($) {
            $("ol li:nth-child(1)").addClass('olli1');
            $("ol li:nth-child(2)").addClass("olli2");
            $("ol li:nth-child(3)").addClass("olli3");
            $("ol li:nth-child(4)").addClass("olli4");
            $("ol li:nth-child(5)").addClass("olli5");
            $("ol li:nth-child(6)").addClass("olli6");
            $("ol li:nth-child(7)").addClass("olli7");
            $("ol li:nth-child(8)").addClass("olli8");
            $("ol li:nth-child(9)").addClass("olli9");
            $("ol li:nth-child(10)").addClass("olli10");
            $("ol li:nth-child(11)").addClass("olli11");
            $("ol li:nth-child(12)").addClass("olli12");
            $("ol li:nth-child(13)").addClass("olli13");
            $("ol li:nth-child(14)").addClass("olli14");
            $("ol li:nth-child(15)").addClass("olli15");
            $("ol li:nth-child(16)").addClass("olli16");
            $("ol li:nth-child(17)").addClass("olli17");
            $("ol li:nth-child(18)").addClass("olli18");
            $("ol li:nth-child(19)").addClass("olli19");
            $("ol li:nth-child(20)").addClass("olli20"); 
         });
        </script>
    
    0 讨论(0)
提交回复
热议问题