繁体   English   中英

javascript在Joomla 3上不起作用

[英]javascript doesn't work on Joomla 3

我总是直接将jquery脚本用于joomla的模块。

最近,我从使用Joomla2切换到了Joomla3。 不知何故,脚本在模块中不再起作用。 有人知道为什么吗?

(尽管某些脚本仍然有效)

示例:这就是我正在研究的内容。

<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>

该代码可以使目标菜单ID平滑滚动。 如果我在Joomla2模块中使用上述代码,则效果很好,但在Joomla 3中不起作用。

任何想法?

默认情况下,Mootools已加载到Joomla中! 2.5.x 这是另一个像jQuery这样的JS库,它们也使用$符号。 因此我们需要使用jQuery.noConflict()方法解决此问题。

尝试以这种方式使用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();
  });

我在这里重写了您的代码:

<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