javascript - 如何获取每个链接的哈希值?

标签 javascript jquery

我正在尝试在每个部分的滚动上编写事件链接状态。

我正在尝试获取链接的哈希值。

我当前的代码是针对这一行 'console.log($(this).eq(i).hash);'返回“未定义”。

谢谢

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Scroll</title>
        <style media="screen">
            body, html {
                padding: 0;
                margin: 0;
            }
            section {
                padding: 6rem;
                text-align: center;
            }
            section:nth-child(1) {
                background: orangered;
            }
            section:nth-child(2) {
                background: steelblue;
            }
            section:nth-child(3) {
                background: orange;
            }
            section:nth-child(4) {
                background: purple;
            }
            section:nth-child(5) {
                background: red;
            }
            section:nth-child(6) {
                background: black;
                color: #fff;
            }

            nav {
                background: black;
                color: #fff;
                padding: 2rem 0;
                text-align: center;
                position: fixed;
                top: 0;
                width: 100%;
            }

            ul {
                display: inline-block;
            }

            ul > li {
                display: inline-block;
                padding-right: 5px;
            }

            .active {
                background: grey!important;
            }
        </style>
    </head>
    <body>
        <nav>
            <ul>
                <a href="#section1">Section 1</a>
                <a href="#section2">Section 2</a>
                <a href="#section3">Section 3</a>
                <a href="#section4">Section 4</a>
                <a href="#section5">Section 5</a>
                <a href="#section6">Section 6</a>
            </ul>
        </nav>
        <section>
            <h2 id="section1">Section 1</h2>
        </section>
        <section>
            <h2 id="section2">Section 2</h2>
        </section>
        <section>
            <h2 id="section3">Section 3</h2>
        </section>
        <section>
            <h2 id="section4">Section 4</h2>
        </section>
        <section>
            <h2 id="section5">Section 5</h2>
        </section>
        <section>
            <h2 id="section6">Section 6</h2>
        </section>
        <script src="https://code.jquery.com/jquery-2.2.4.min.js"   integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
        <script type="text/javascript">
        $("a[href^='#']").each(function(i){
            console.log($(this).eq(i).hash);
        });
            $(window).scroll(function(){
                $("section").each(function(i){
                    if( $(document).scrollTop() >= $("section").eq(i).offset().top) {
                        $("section").eq(i).addClass("active");
                    } else {
                        $("section").eq(i).removeClass("active");
                    }
                });
            });
        </script>
    </body>
</html>

最佳答案

jquery 对象没有 hash 属性,底层 dom 元素有

而不是

$(this).eq(i).hash

尝试

this.hash

关于javascript - 如何获取每个链接的哈希值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40001894/

相关文章:

javascript - 从 console.log 调用 webpack 捆绑函数/类

javascript - html 表单在分辨率更改时更改位置

javascript - 如何获取最后一个元素并知道 id

javascript - jquery 鼠标事件的多种函数

javascript - 输入数据未定义

jQuery ajax (jsonp) 忽略超时并且不触发错误事件

javascript - Jquery获取列表元素的id并根据点击调用函数

javascript - 如何使用 rich :calendar 在日期选择上显示 timeEditor

javascript - Node JS动态集合ajax调用异步等待

jquery - DataTables 处理消息卡在网格重新加载上