Href标签不在chrome中工作,但在其他浏览器中工作

时间:2017-11-27 17:53:54

标签: javascript jquery html google-chrome

我正在设计网站,但面临一个问题,即href#tags在chrome中不起作用但在firefox中工作



<ul class="navigation" style="margin-top: 75px;">
   <li><a class="scroll-to" href="#section-1">Home</a></li>
   <li><a class="scroll-to" href="#section-2">About Us</a></li>
   <li><a class="scroll-to" href="#section-4">Products</a></li>
   <li><a class="scroll-to" href="#section-5">Clients</a></li>
   <li><a class="scroll-to" href="#section-6">Team</a></li>
   <li><a class="scroll-to" href="#section-7">Contact Us</a></li>
</ul>


<section id="section-1" class="banner-container color-light center nav-trigger">
&#13;
&#13;
&#13;

我不确定它出了什么问题

5 个答案:

答案 0 :(得分:2)

以下在Chrome 62中对我来说很好。您是否关闭了部分代码?你确定它有足够的高度可以实际滚动吗?

section {
   padding: 100px;
   border: 1px solid blue;
}
<ul>
    <li><a href="#section-1">Home</a></li>
    <li><a href="#section-2">About Us</a></li>
    <li><a href="#section-4">Products</a></li>
    <li><a href="#section-5">Clients</a></li>
    <li><a href="#section-6">Team</a></li>
    <li><a href="#section-7">Contact Us</a></li>
</ul>
<section id="section-1">Home</section>
<section id="section-2">About Us</section>
<section id="section-4">Products</section>
<section id="section-5">Clients</section>
<section id="section-6">Team</section>
<section id="section-7">Contact Us</section>

答案 1 :(得分:1)

你可以试试这个。 https://www.gregoryvarghese.com/chrome-anchor-link-not-working-fix/

$(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;
       }
     }
   });
 });

答案 2 :(得分:0)

确保每个div都有足够的高度,以便页面可以滚动。如果没有页面滚动,您就无法看到更改,因为您的代码没有任何问题。

<ul>
   <li><a href="#section-1">Home</a></li>
   <li><a href="#section-2">About Us</a></li>
   <li><a href="#section-4">Products</a></li>
   <li><a href="#section-5">Clients</a></li>
   <li><a href="#section-6">Team</a></li>
   <li><a href="#section-7">Contact Us</a></li>
</ul>
<section style="height:500px" id="section-1">Home</section>
<section style="height:500px" id="section-2">About Us</section>
<section style="height:500px" id="section-4">Products</section>
<section style="height:500px" id="section-5">Clients</section>
<section style="height:500px" id="section-6">Team</section>
<section style="height:500px" id="section-7">Contact Us</section>

答案 3 :(得分:0)

我猜这是某些Chrome版本中的一个错误,这个解决方法对我有用,祝你好运! -

$(document).ready(function () {
    var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
    if (window.location.hash && isChrome) {
        setTimeout(function () {
            var hash = window.location.hash;
            window.location.hash = "";
            window.location.hash = hash;
        }, 300);
    }
});

答案 4 :(得分:0)

对我来说,是href重定向到带有#标签的页面,所以我做了以下解决方法:

$('body').on('click', 'a[href*="#"]:not([href="#"])', function(e) {
    if($(this).attr('href').indexOf('http') > -1 && /chrome/i.test( navigator.userAgent) ){
        e.preventDefault();
        window.location.href = $(this).attr('href');
        window.location.reload();
        return false;
    }
});