javascript - 如何修复 anchor 标签着陆在错误位置的问题

标签 javascript jquery html

我是 JavaScript 新手。抱歉,如果这是重复的,但我找不到任何满足我需求的解决方案。

当点击 anchor 标签时,它会引导到我想要的位置。但是,我有另一个 anchor 标记,旨在链接到与另一个 anchor 标记相同的位置。如果我单击此按钮(在我已经单击了另一个 anchor 标记,该 anchor 标记与我现在要单击的位置相同),它不会引导我到达所需的位置。

我知道这一定是因为我正在使用 hashchange。我尝试使用 click 但在使用 hashchange< 时返回 -1 for console.log(document.URL.indexOf(“#”)); 而不是 58/.

我认为点击逻辑是在正确的路径上,但我不明白为什么它没有达到我的预期。 (即:点击页面上可点击的内容后,点击运行)。

我还尝试使用 setTimeout 但我无法找到一种方法来正确操作它来“重新启动”我的 fixedAnchor 函数。

    function fixedAnchor() {
        // if (location.hash.length !== 0) {
            window.scrollTo(window.scrollX, window.scrollY - 130);
            console.log(document.URL.indexOf("#"));
            // console.log(document.URL.indexOf("#") > -1 & document.URL.indexOf("#") <= 58);
            // if (document.URL.indexOf("#") > -1 & document.URL.indexOf("#") < 58) {
            //  console.log(document.URL.indexOf("#") > -1);
                offSetAnchor();
            // }
        // }
    }

    function offSetAnchor() {
        if (document.URL.indexOf("#") != 58){
            window.scrollTo(window.scrollX, window.scrollY - 130);
        }
        // setTimeout(function(){
        //  window.scrollTo(window.scrollX, window.scrollY - 130);
        //  // fixedAnchor();
        // }, 1000);
    }

    window.addEventListener('hashchange', fixedAnchor);
    // window.addEventListener('click', fixedAnchor);

最佳答案

这可以通过仅使用 HTML 的命名 anchor 来完成。请查找下面的代码,其中 2 个链接位于同一位置

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>

  <body>
    <a href="#gohere">click 1 to gohere</a>
    <p>Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
    <p><a href="#gohere">click 1 to gohere</a>Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
    <p>Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
    <p>Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
    <p id="gohere"> Selected Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

</p>
  </body>

</html>

关于javascript - 如何修复 anchor 标签着陆在错误位置的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37818261/

相关文章:

javascript - .map() 函数并使用 ReactJS 从表中删除一行

javascript - jQuery.each 用于列表和非列表

javascript - 相对于窗口缩放 CSS

html - 如何通过将图像悬停在另一个 div 中来更改悬停在一个 div 中的图像?

javascript - 如何根据 PHP 端的 bool 变量通过 AJAX 更新和加载不同的 div block ?

javascript - 如何使用 Jquery/Javascript AutoComplete 解码 HTML 实体

javascript - 如何在相邻的 iframe 内调用 javascript 函数

php - html 和 php 表单到数据库输入 0000-00-00 作为日期,而不是所选的 html5 日期选择器日期

Android Jelly Bean 及以上版本上 Webview 的 JavascriptInterface

jquery - 在文档中使用 Jquery $.get 准备就绪