繁体   English   中英

Href标签不适用于Chrome,但适用于其他浏览器

[英]Href tag not working in chrome but working in other browser

我正在设计网站,但遇到的问题是href#标签在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"> 

我不确定哪里出了问题

以下内容对我在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> 

您可以尝试遵循此步骤。 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;
       }
     }
   });
 });

确保每个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>

我猜这是某些版本的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);
    }
});

对我来说,是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;
    }
});

暂无
暂无

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

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