简体   繁体   中英

javascript doesn't work on Joomla 3

I always use jquery script directly to joomla's modules.

Lately, I switched from using Joomla2 to Joomla3. Somehow, scripts are not working anymore in modules. Anybody knows why?

(some script still works though)

Example: This is what I am working on.

<a href="#intro">Intro</a> <a href="#about">About</a> <a href="#info">Info</a>
<h1 id="intro">Intro</h1>
<p>abcd</p>
<h1 id="about">About</h1>
<p>xxxxxxxxxx</p>
<p>xxxxxxxxxx</p>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var jump=function(e)
{
       //prevent the "normal" behaviour which would be a "hard" jump
       e.preventDefault();
       //Get the target
       var target = $(this).attr("href");
       //perform animated scrolling
       $('html,body').animate(
       {
               //get top-position of target-element and set it as scroll target
               scrollTop: $(target).offset().top
       //scrolldelay: 2 seconds
       },600,function()
       {
               //attach the hash (#jumptarget) to the pageurl
               location.hash = target;
       });

}

$(document).ready(function()
{
       $('a[href*=#]').bind("click", jump);
       return false;
});
</script>

The code gives a smooth scroll on targeted menu id. If I use above code in Joomla2 module, it works great but doesn't work in Joomla 3.

Any idea?

Mootools is loaded by default in Joomla! 2.5.x. It's an another JS library like jQuery they also use $ symbol. so we need to fix this issue using the jQuery.noConflict() method.

Try to use jQuery this way and recheck.

  var $jQ = jQuery.noConflict();
  // $jQ is now an alias to the jQuery function
  // creating the new alias is optional.

  $jQ(document).ready(function() {
   $jQ( "div" ).hide();
  });

I have rewritten your code here:

<script type="text/javascript">
var jump=function(e)
{
       //prevent the "normal" behaviour which would be a "hard" jump
       e.preventDefault();
       //Get the target
       var target = $jQ(this).attr("href");
       //perform animated scrolling
       $jQ('html,body').animate(
       {
               //get top-position of target-element and set it as scroll target
               scrollTop: $jQ(target).offset().top
       //scrolldelay: 2 seconds
       },600,function()
       {
               //attach the hash (#jumptarget) to the pageurl
               location.hash = target;
       });

}

var $jQ = jQuery.noConflict();

$jQ(document).ready(function()
{
       $jQ('a[href*=#]').bind("click", jump);
       return false;
});
</script>

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