简体   繁体   中英

Simple jQuery addClass not working

I have this short code,

$("#contact").click(function(){
$("#contact").addClass("clicked");
});

And it is not working, returning me

"Uncaught TypeError: $ is not a function"

, ref to the 2nd line

$("#contact").addClass("clicked");

So, jquery is working cause he is recognizing the first line, and the element $("#contact") is correctly named cause he is detecting the click function. I can´t guess why is that line failing.


After seeing all your comments,

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function(){
            $("#contact").click(function(){
                $("#contact").addClass("clicked");
            });
        });
        </script>

My code is just like that. I think jquery is well inserted, and its all in the head of the page

If you have included jQuery but $ is undefined, you may run jQuery in noConflict mode. Than you can wrap your code like this:

jQuery(function($) {
    $("#contact").click(function(){
        $("#contact").addClass("clicked");
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM