Why does jQuery throw the error `fadeOut is not a function'?

后端 未结 10 775
感情败类
感情败类 2020-12-01 17:59

I am using jQuery and put this code in my javascript:

function HideMe(itemID) {
    var myDiv = \'item_\' + itemID;
    $(myDiv).fadeOut(\"slow\");
}
         


        
相关标签:
10条回答
  • 2020-12-01 18:11

    It looks like jquery is not correctly attached to the page.

    Check your linking to jQuery.

    0 讨论(0)
  • 2020-12-01 18:18

    It happened to me because of slim version of Jquery library. Full version of Jquery library includes animation.

    Jquery CDN available at https://code.jquery.com/

    0 讨论(0)
  • 2020-12-01 18:21

    Try keeping it inside

    $(document).ready(function(){
    // your code. and don't forget the '#' in front of item.
    });
    

    Looks like you're trying to call the function before jQuery / the DOM loads.

    0 讨论(0)
  • 2020-12-01 18:24

    I had this error because I was using a slim version of jQuery. If you download the full version you should be okay.

    0 讨论(0)
  • 2020-12-01 18:27

    Even if the selector didn't return any items in the collection the function call would have worked (not generated this error anyway) if jQuery was loaded correctly. Either there is a conflict in the page, or it didn't load at all. You can try

    jQuery(myDiv).fadeOut("slow");
    

    or look into why jQuery hasn't been loaded.

    P.S.: don't forget the # in the selector if selecting by id.

    0 讨论(0)
  • 2020-12-01 18:31

    Also, you probably forgot a # in the selector (unless you've got something like <item_1 /> in the markup).

    var myDiv = '#item_' + itemID;
    

    jQuery uses CSS selectors to search for elements, so without the #, you'd get every element with the tag item_x instead of the ID.

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