简体   繁体   English

javascript在Joomla 3上不起作用

[英]javascript doesn't work on Joomla 3

I always use jquery script directly to joomla's modules. 我总是直接将jquery脚本用于joomla的模块。

Lately, I switched from using Joomla2 to Joomla3. 最近,我从使用Joomla2切换到了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. 该代码可以使目标菜单ID平滑滚动。 If I use above code in Joomla2 module, it works great but doesn't work in Joomla 3. 如果我在Joomla2模块中使用上述代码,则效果很好,但在Joomla 3中不起作用。

Any idea? 任何想法?

Mootools is loaded by default in Joomla! 默认情况下,Mootools已加载到Joomla中! 2.5.x. 2.5.x It's an another JS library like jQuery they also use $ symbol. 这是另一个像jQuery这样的JS库,它们也使用$符号。 so we need to fix this issue using the jQuery.noConflict() method. 因此我们需要使用jQuery.noConflict()方法解决此问题。

Try to use jQuery this way and recheck. 尝试以这种方式使用jQuery并重新检查。

  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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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