[英]Return current URL for a share button
我是 Web 開發的新手,我正在嘗試使用 JS 將當前 URL 插入到 href 以獲取 Facebook 共享按鈕。 我嘗試了以下方式,但如您所見,這是不正確的。
function getURL() { return window.location.href; }
<div class="fb-share-button" data-href="https://www.facebook.com" data-layout="button" data-size="small"> <a target="_blank" href="getURL()" class="fb-xfbml-parse-ignore"> Share </a> </div>
嘗試這個
<script>
document.querySelector('a').setAttribute("href", window.location.href);
</script>
<div class="fb-share-button" data-href="https://www.facebook.com" data-layout="button" data-size="small"><a target="_blank" href="" class="fb-xfbml-parse-ignore" id='facebook-share'>Share</a></div>
在您的標簽<a>
添加id
<div class="fb-share-button" data-layout="button" data-size="small"><a target="_blank" id="btnsharefb" class="fb-xfbml-parse-ignore">Share</a></div>
並建立監聽器事件點擊你的標簽a
document.getElementById("btnsharefb").addEventListener("click", function() {
this.href = getURL();
});
或直接在加載文檔上添加
document.getElementById("btnsharefb").href = getURL();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.