【问题标题】:click a href link automatically on page load depending on hash location根据哈希位置在页面加载时自动单击 href 链接
【发布时间】:2015-05-05 18:14:33
【问题描述】:

我在 HTML/JS 中有这段代码:

<a href="#customer_tickets" class="tabLink" onclick="LoadPage('/customer/editcustomer_tickets.php?customer=<?php echo $customer["sequence"]; ?>', '.EditCustomer');">Tickets</a>

if(location.hash) {
        //load the set hash here
}

点击a href时,会运行JS中的LoadPage函数将页面加载到div中

当它们被点击时,它会在浏览器的 URL 中设置哈希位置

我希望能够检查页面加载时是否设置了哈希位置,如果是,则自动单击正确的 a href

我该怎么做?

【问题讨论】:

  • 如果 location.hash 为真,为什么不尝试将页面加载到 div 中?

标签: javascript jquery


【解决方案1】:

使用location.hash 的值来识别正确的a 元素,然后点击它:

//Wait for the document to be ready
$(function() {
    //Check if we have a hash, if so try to click an element that links to it
    if (location.hash.length > 0) {
        $('a[href="' + location.hash + '"]').click();
    }
});

【讨论】:

    猜你喜欢
    • 2012-06-23
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多