HTML href 变量值

HTML href variable value

更新: example of code at W3school tryit editor

在我的网页上,我有 20 篇文章的供稿,每篇文章下面都有一个社交媒体共享面板,用于分享每篇文章,代码如下:

<a href="http://www.facebook.com/sharer.php?u=HTTPURLTOSHARE">
  <i class="fab fa-facebook"></i>
</a>

所以基本上我喜欢每个社交媒体服务(比如 10、FB、TW、IG 等)的几个这样的链接以及每篇文章的整个链接。
我的问题是:有没有一种方法可以编写包含所有共享服务链接的 SHARING DIV 单击共享按钮时对每篇文章一次并分配它为 URL (HTTPURLTOSHARE)到社交媒体分享按钮,?
我知道这是可能的服务器端,但我不熟悉前端后端开发。

这很重要

  1. 我做了一个模板
  2. 我给弹出 links 一个带有文章 URL 的数据属性
  3. 我在每个link
  4. 后面插入模板内容

window.addEventListener("load",function() {
  [...document.querySelectorAll("a[data-url]")]
    .forEach((lnk,i) => {
      const url = lnk.dataset.url;
      const pop = document.getElementById("sharer").content.cloneNode(true)
      pop.querySelector('.overlay').setAttribute("id",lnk.hash.slice(1)); 
      [...pop.querySelectorAll(".social-square a")].forEach(lnk => lnk.href = lnk.href.replace("SHARE",url))
     lnk.after(pop)
    })
})
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"  />
<!-- FIRST ARCITLE -->
<h1>We must stop calling Trump’s enablers ‘conservative.’ They are the radical right.</h1>
<p>You hear the word “radical” a lot these days. It’s usually aimed like a lethal weapon at Democratic office-seekers, especially those who want to unseat a Republican incumbent. Sen. Kelly Loeffler, the Georgia Republican, rarely utters her challenger’s name without branding him as “radical liberal Raphael Warnock.”</p>
<a href="#popup1" data-url="https://www.washingtonpost.com/lifestyle/media/trump-enablers-radical-right-conservative/2021/01/04/634edcda-4e97-11eb-b96e-0e54447b23a1_story.html">Share this article</a>


<!-- SECOND ARTICLE -->
<h1>An Insurgency From Inside the Oval Office</h1>
<p>President Trump’s effort to overturn the election he lost has gone beyond mere venting of grievances at the risk of damaging the very American democracy he is charged with defending.</p>
<a href="#popup2" data-url="https://www.nytimes.com/2021/01/04/us/politics/trump-biden-inauguration.html">Share this article</a>


<template id="sharer">
<!-- HERE START THE SHARING MODAL WINDOW and it will be repeated for each article, Only the article URL change so this is why i want to find a way to write this code once and assign only the URL for each article if possible -->
<div id="" class="overlay">
    <div class="popup"><a class="close" href="#"><i class="fas fa-times-circle"></i></a>
        <div class="content">
            <div class="shr-div">
                <!-- Facebook -->
                <div class="social-square fac" data-content="facebook">
                    <a href="http://www.facebook.com/sharer.php?u=SHARE" target="_blank"><i class="fab fa-facebook-f"></i></a>
                </div>
                <!-- Twitter -->
                <div class="social-square twi" data-content="twitter">
                    <a href="https://twitter.com/share?url=SHARE" target="_blank"><i class="fab fa-twitter"></i></a>
                </div>
                <!-- WhatsApp -->
                <div class="social-square wha" data-content="whatsapp">
                    <a href="whatsapp://send?text=SHARE" target="_blank"><i class="fab fa-whatsapp"></i></a>
                </div>
                <!-- Viber -->
                <div class="social-square vib" data-content="viber">
                    <a href="viber://forward?text=SHARE" target="_blank"><i class="fab fa-viber"></i></a>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- END OF THE SHARE MODAL WINDOW -->
</template>

我认为您需要对社交提要进行(http 调用)以获取文章详细信息(包括链接),然后一旦您拥有数据,您就可以整合并在您的页面中呈现链接。