Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 相同和不同页面的锚定链接_Javascript_Jquery_Html_Anchor - Fatal编程技术网

Javascript 相同和不同页面的锚定链接

Javascript 相同和不同页面的锚定链接,javascript,jquery,html,anchor,Javascript,Jquery,Html,Anchor,我使用的代码是: $(function() { $('a[href*=#]:not([href=#])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var target = $(this.hash); target =

我使用的代码是:

$(function() {
  $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});
链接到div

问题是: “#allcontentcontact”在我的主页上,因此代码只有在我在主页上时才能完美工作。但我有一个固定的菜单链接到其他页面!所以当我在另一个页面上时,我需要一些东西使我的a href链接到那个div


谢谢

谢谢你的回答,我做到了!:)


html代码在哪里?我想我已经发布了所有重要的内容。你还需要看什么?
<script>
var url = "http://192.168.1.157/wp/";
$(function(){
  if (location.href==url){
    $('#menu #mylink').attr("href","#bluecontent");
    $('#menu #mylink2').attr("href","#allcontentcontact");
  }
});
</script>
<a id="mylink" href="<?php bloginfo('url'); ?>#bluecontent"><span>Parceiros</span></a>

<a id="mylink2" href="<?php bloginfo('url'); ?>#allcontentcontact"><span>Contato</span></a>
var jump=function(e)
{
   if (e){
       e.preventDefault();
       var target = $(this).attr("href");
   }else{
       var target = location.hash;
   }

   $('html,body').animate(
   {
       scrollTop: $(target).offset().top
   },2000,function()
   {
       location.hash = target;
   });

}

$('html, body').hide();

$(document).ready(function()
{
    $('a[href^=#]').bind("click", jump);

    if (location.hash){
        setTimeout(function(){
            $('html, body').scrollTop(0).show();
            jump();
        }, 0);
    }else{
        $('html, body').show();
    }
});