Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.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
在html中添加锚定标记和潜在的javascript干扰问题_Javascript_Html_Google Chrome_Anchor - Fatal编程技术网

在html中添加锚定标记和潜在的javascript干扰问题

在html中添加锚定标记和潜在的javascript干扰问题,javascript,html,google-chrome,anchor,Javascript,Html,Google Chrome,Anchor,我有两个html页面,在第一个页面上有一个指向第二个页面的锚定标记的链接 这是第一页。在页面底部有一些链接。如果用户点击例如模拟电子设备链接,他/他将进入带有锚定标签的第二页 以下是第一页上的链接: <a href="/electronics.html#electronics-books-suggested-by-hooman-darabi">Electronics</a> 起初,锚定不是在Chrome上工作,而是在Firefox上工作。在谷歌搜索之后,我找到了一些线索

我有两个html页面,在第一个页面上有一个指向第二个页面的
锚定标记的链接

这是第一页。在页面底部有一些链接。如果用户点击例如
模拟电子设备
链接,他/他将进入带有
锚定标签的第二页

以下是第一页上的链接:

<a href="/electronics.html#electronics-books-suggested-by-hooman-darabi">Electronics</a>
起初,锚定不是在Chrome上工作,而是在Firefox上工作。在谷歌搜索之后,我找到了一些线索:

在html文件的底部,我有三个JavaScript文件:

如果我注释掉这3个选项中的任何一个,那么
锚定
在Chrome和FireFox上都可以正常工作。但是我当然需要在我的页面上包含这3个JS文件

经过进一步研究,我发现这个建议:

为了让
锚定
工作,当点击链接时,显示的第二页顶部将位于
锚定标记
位置,我必须将此代码添加到html页面:

$(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;
                }, 200);
            }
        });
我不知道这段代码是做什么的,但它有助于将被锚定的内容显示在显示的链接页面的顶部

虽然这段代码解决了我的问题,但现在有些不好的事情发生了!。如果在第二页()中,我单击Chrome背面底部,我不会返回上一页。我需要按3次后底部才能返回上一页。如果我看一下每次按下后,URL会发生什么变化,我会在页面的URL中看到:

原始URL:

http://www.doradolist.com/analog.html#analog-electronics-books-suggested-by-hooman-darabi
单击一次后底按钮后:

http://www.doradolist.com/analog.html#
在两次后底部单击后:

http://www.doradolist.com/analog.html#analog-electronics-books-suggested-by-hooman-darabi
最后,在3次点击后,我转到原始页面


如果您能帮助我们了解为什么会出现这种“后底”问题以及如何解决它,我们将不胜感激。或者,如果不是添加我上面展示的代码,而是有另一种方法来解决Chrome中的
锚定问题,那会更好,因为后端问题是该代码的结果。

要回答您关于为什么会发生这种情况的问题,我将解释这段代码在做什么

一旦您登录到第二个页面(历史上的第一个URL),此代码段将等待加载文档(
$(document).ready
),然后执行传递给它的回调,检查浏览器是否为Chrome,如果是Chrome,则将回调添加到消息总线(
setTimeout
),从URL获取哈希值,它从URL(历史上的第二个URL)中删除散列,然后将其添加回URL(历史上的第三个URL,以及最终“滚动”到该ID的URL)

我已经用注释注释了您的代码

$(document).ready(function () { // wait for document to load
            var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor); // check if browser is Chrome
            if (window.location.hash && isChrome) { // if the URL has a hash in it, and the browser is Chrome
                setTimeout(function () { // add this callback to the message bus
                    var hash = window.location.hash; // read the hash from the URL in the address bar
                    window.location.hash = ""; // set the hash in the URL in the address bar to be empty
                    window.location.hash = hash; // add the hash back to the URL in the address bar
                }, 200);
            }
        });

谢谢,杰克,这是一个很好的解释。现在我如何解决这个问题?点击后退按钮三次返回是非常烦人的。同时,由于你精通JavaScript,你能解释一下为什么如果我注释掉html文件中包含的3个JS代码中的任何一个,锚定问题就会消失吗?有没有办法通过在某个地方添加延迟来解决这个问题呢?似乎发生的情况是,您包含的其他三个脚本中的一个从URL中删除了片段(散列)。我简要地看了一下这些脚本的源代码,它看起来像是
main.js
中有一些东西可以删除/更改
window.location.hash
。我现在正在打电话,但如果之前没有人接,我会稍后再仔细看一看。如果你能帮忙的话,我将不胜感激。
$(document).ready(function () { // wait for document to load
            var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor); // check if browser is Chrome
            if (window.location.hash && isChrome) { // if the URL has a hash in it, and the browser is Chrome
                setTimeout(function () { // add this callback to the message bus
                    var hash = window.location.hash; // read the hash from the URL in the address bar
                    window.location.hash = ""; // set the hash in the URL in the address bar to be empty
                    window.location.hash = hash; // add the hash back to the URL in the address bar
                }, 200);
            }
        });