Joomla 2.5 Jquery Cannot call method of null

2019-02-20 22:14发布

问题:

Hi guys I am using Joomla 2.5 with Jquery and I got this strange error in my chrome console:

"Uncaught TypeError: Cannot call method 'slideUp' of null "

here is index.php header section:

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <script type="text/javascript" src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template ?>/js/jscript.js"></script>
    <jdoc:include type="head" />

and jscript.js code:

$(document).ready(function(){

    var slider = document.getElementById("login-form");

    alert (slider);

    $("#login-form").slideUp(100); <- error is on this line

});

I tested it and alert showed : [object HTMLFormElement], so it meas "login-form" realy exists. can't figure out why am I getting

"Uncaught TypeError: Cannot call method 'slideUp' of null "

Please, can you help me!!!!!!

回答1:

Most probably your jquery code is conflicting with mootools. Try this using

jQuery.noConflict();

Your code will be like below-

var $j = jQuery.noConflict();
$j(document).ready(function(){

    var slider = document.getElementById("login-form");

    alert (slider);

    $j("#login-form").slideUp(100);

});