在单击链接而不是哈希时传递属性



我的菜单中有一个链接。它有一个像title=.roc这样的属性。 当我单击此链接时,我想保存此属性并单击目标页面上具有相同属性的元素。

example.com页 .我点击下一个链接。

<li class="menu-item"><a title=".roc" href="https://example.com/port/#flash">Roc</a></li>

所以现在,在example.com/port/中,它应该单击带有title=".roc"的元素:

<a id="flash" href="#" title=".roc">ROC2</a>

我有这段代码,但我不知道如何传递属性而不是哈希:

jQuery(function ($) {
var activeTab = document.querySelector(location.hash);
history.replaceState(null, null, ' ');
window.addEventListener('load', function () {
if (activeTab) {
setTimeout(function(){ activeTab.click(); }, 100);
}  
})
});

在您想要从中挂钩传输"标题"的页面上:

jQuery(function ($) {
$(document.body).on('click','a[title]', function(event) {
sessionStorage.setItem('title', event.target.getAttribute('title'));
});
});

然后在另一页上:

jQuery(function ($) {
var activeTab = document.querySelector(`[title="${sessionStorage.getItem('title')}"]`);
window.addEventListener('load', function (event) {
if (activeTab) {
setTimeout(function(){ activeTab.click(); }, 100);
}  
})
});

最新更新