[英]Javascript var in href tag
嗨,我想在帶有 window.location.href 的 href 標簽中放置一個動態鏈接
我怎樣才能做到這一點?
<a href="https://twitter.com/share?url="+ <script> window.location.href </script>+ "&text=!" target="_blank">
Twitter
</a>
你可以給a
ID和由查找。
document.getElementById('foo').setAttribute('href', `https://twitter.com/share?url="${window.location.href}&text=!`);
<a id="foo" target="_blank"> Twitter </a>
您可以嘗試以下方法:
var el = document.querySelector('a'); el.href = el.href+window.location.href + '&text=!';
<a href="https://twitter.com/share?url=" target="_blank"> Twitter </a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.