简体   繁体   English

jquery 触发点击事件依赖于 url hash

[英]jquery Triggering click event dependent on url hash

I'm trying to create a function that clicks a tab if the hash is equal to a certain value.如果 hash 等于某个值,我正在尝试创建一个 function 来单击一个选项卡。 I can get it to alert from a hash but every time that I attempt to have the hash result in a click I get no response.我可以让它从 hash 发出警报,但每次我尝试让 hash 导致点击时,我都没有得到响应。 I'm newer to this so I'm sure it's something simple.我对此比较陌生,所以我确信这很简单。

Here's what I'm trying:这是我正在尝试的:


$("#nav-page_requests-tab").click(function() {
            window.location.hash = '2';
        });

var currentValue = window.location.hash.substr(1);
        $(document).ready(function() {
            if(currentValue == '2'){
                $("#nav-page_requests-tab").click();
            }
        });

Here's the HTML:这是 HTML:

<a class="nav-item nav-link" id="nav-page_requests-tab" data-toggle="tab" href="#nav-page_requests" role="tab" aria-controls="nav-page_requests" aria-selected="false">Page Requests</a>

Move your click event binding inside ready function like:将您的点击事件绑定移动到准备好的 function 中,例如:

var currentValue = window.location.hash.substr(1);
        $(document).ready(function() {
           $("#nav-page_requests-tab").click(function() {
             window.location.hash = '2';
           });

           if(currentValue == '2'){
              $("#nav-page_requests-tab").click();
           }
        });

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

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