jQuery “Uncaught TypeError: undefined is not a function” when using fadeIn();

北城余情 提交于 2019-12-23 20:56:10

问题


I am new to JS and I am writing a basic jQuery-rich webpage with fade-in/fade-out for each page in the same document (using the same div element with separate IDs). Anyway, when I try to fade in the current page, I receive the error "Uncaught TypeError: undefined is not a function", I've had a search around online but couldn't find a solution.

The HTML is loaded before the script and the full jQuery library has been included in the header of the page.

The div element I am targeting has "display: none;" set in my CSS.

HTML

<div class="page_content" id="page_home">
Content removed
</div>

JS:

<script>

var cp = "page_home";
var tp = document.getElementById(cp);
tp.fadeIn('slow');

</script>

Anyone experienced anything similar and have a solution? I'm sure it's something simple, since my code is anyway...


回答1:


tp is a plain javascript object and it does not has a function called .fadeIn()

Try to wrap it inside $() to make it a jquery object then do,

$(tp).fadeIn('slow');

or better use jquery's id selector to grab the element,

$("#page_home").fadeIn('slow');



回答2:


Because tp is not a jquery object, it will not have the methods related to jquery.

Use,

$("#page_home").fadeIn('slow');


来源:https://stackoverflow.com/questions/24573690/jquery-uncaught-typeerror-undefined-is-not-a-function-when-using-fadein

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!