简体   繁体   English

Chrome上区分大小写的锚点问题

[英]Case sensitive anchor issue on Chrome

Case sensitive links don't work on chrome. 区分大小写的链接不适用于chrome。 If name of anchor is link#Anchor1 will not work, and link#anchor1 will. 如果锚点名称为link#Anchor1将不起作用,而link#anchor1将起作用。

I went ahead and searched for solution and found .toLowerCase() function. 我继续搜索解决方案,发现.toLowerCase()函数。 Now I have a new issue I couldn't fix: Uncaught TypeError: anchorEl.offset is not a function 现在我有一个新的问题我无法解决:Uncaught TypeError:anchorEl.offset不是一个函数

  $(window).load(function()
  {
    var hashVal= window.location.hash.substring(1).toLowerCase();
    var anchorEl = $("a[name='"+hashVal+"']").toLocaleString().toLowerCase().split(',');
    var elOffset = anchorEl.offset();
    var offsetTop = elOffset.top;
    $(document).scrollTop( offsetTop - 200);
    console.log(offsetTop);

  });

It partially works on Firefox, except for the offset value, either +200 or -200 it won't make a difference (it's behind the navigation bar) but it doesn't move a inch on Chrome. 它可以部分在Firefox上运行,除了偏移值+200或-200之外,它没有任何区别(位于导航栏后面),但在Chrome上不会移动一英寸。

Try like below. 尝试如下。 First select all the a elements with name attribute and then filer with comparing to windows hash. 首先选择具有name属性的所有a元素,然后将其与Windows哈希进行比较。

var anchorEl = $("a[name]").toArray().filter(function(a) {
    return $(a).attr('name').toLowerCase() == hashVal.toLowerCase();
});
var elOffset = $(anchorEl).offset();

Checkout code below. 结帐代码如下。 It's scrolls to 300px; 滚动到300px;

 $(window).load(function() { var hashVal = "Action"; //window.location.hash.substring(1).toLowerCase(); var anchorEl = $("a[name]").toArray().filter(function(a) { return $(a).attr('name').toLowerCase() == hashVal.toLowerCase(); }); if (anchorEl.length > 0) { var elOffset = $(anchorEl).offset(); var offsetTop = elOffset.top; $(document).scrollTop(offsetTop - 200); console.log(offsetTop); } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <div style="margin-top: 300px;"> <a href="#" name="action"> action </a> </div> <div style="margin-bottom: 300px;"> <a href="#" name="action2"> action2 </a> </div> 

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

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