【问题标题】:HTML href variable valueHTML href 变量值
【发布时间】:2021-04-10 13:41:40
【问题描述】:

更新: 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) 分配给社交媒体共享按钮,?
我知道这可能是服务器端,但我不熟悉前端开发。

【问题讨论】:

  • 你知道吗!对我来说,为每个共享 div 重复一行大概是每篇文章大约 100 行,这不是问题,因为它将通过桌面 Windows 应用程序自动完成,我只给他文章链接,然后他获取信息将每篇文章的代码注入页面,我的问题是我在一个网页中以 50 篇文章的 5000 行代码结束,我认为这会影响页面加载???

标签: html variables constants href non-repetitive


【解决方案1】:

这不是小事

  1. 我做了一个模板
  2. 我为弹出链接提供了带有文章 URL 的数据属性
  3. 我在每个链接后插入模板内容

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>

【讨论】:

  • 我现在没有得到它,我在 tryiteditor 上发布了我的代码链接的更新
  • 我刚刚尝试过,它可以工作,但你说我必须在每个文章链接下复制模板???我只是将您的示例与一个模板一起使用,它可以正常工作!!!
  • 一切都很好,它只是做它应该做的,非常感谢。答案已接受
  • 一个模板 - 链接来自弹出链接的数据属性
  • 我改进了代码以从打开它的链接中获取覆盖 ID pop.querySelector('.overlay').setAttribute("id",lnk.hash.slice(1));
【解决方案2】:

我认为您将需要(http 调用)到社交信息源以获取文章详细信息(包括链接),然后在获得数据后,您可以合并并呈现页面中的链接。

【讨论】:

  • 这真的是评论,而不是答案。多一点代表,you will be able to post comments.
  • 是的,它是一条评论,但我无法添加评论,正如您正确指出的那样。使用您建议的方法,所有社交链接都将被渲染,然后使用 js 合并和重新渲染 ..
  • 否,因为文章已经由基于 Windows 的应用程序获取并直接注入网页,包括 URL、描述、日期和 OG 图片,
  • 所以你已经有了所有的数据,那么它应该更直接..
  • @CarolineD。我创建了一个 Windows 软件,在其中我选择文章给他 URL,它只获取它们(标题、描述、OG 图像、链接等)并为每篇文章创建 HTML 代码,包括每篇文章下的 Share DIV 元素的 100 行代码,我最终一页有 5000 行或更多行,这就是问题
猜你喜欢
  • 2021-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多