簡體   English   中英

Javascript-獲取鏈接href

[英]Javascript - get link href

這是我的代碼:

function overlay(event) {
    event.preventDefault();
    el = document.getElementById("overlay");
    el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
    var href = event.target.href;
}

但是,當我將其與這樣的HTML結合使用時:

    <a href="https://www.somesite.com/" title="Example link" onclick="overlay(event);">Example text</a>
    <!-- ... -->
    <a href="#" onclick="window.location.href = href;">Go!</a>

因此,我想問一下如何獲取第一個鏈接的href並將其發送給第二個鏈接的onclick事件?

在第一個錨點元素上放置一個id

<a id="TargetAnchor" href="https://www.somesite.com/" title="Example link" onclick="overlay(event);">Example text</a>

然后將其定位到第二個點擊處理程序中

<a href="#" onclick="window.location.href = document.getElementById('TargetAnchor').href;">Go!</a>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM