我有 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
的值来识别正确的 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();
}
});
关于javascript - 根据哈希位置,在页面加载时自动单击 href 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28859856/