【问题标题】:How to add two html scripts in a single blogger post?如何在一篇博文中添加两个 html 脚本?
【发布时间】:2021-08-15 01:55:33
【问题描述】:

我在博客中拥有自己的博客,并且我是内容提供商。我在我的博客文章中为我的某些下载链接使用了内容锁定代码。一切都很顺利,直到我偶然发现了这一点。在一篇文章中,有两个以上的下载链接,我尝试再次添加相同的脚本。该脚本实际上是一个订阅解锁脚本,观众需要订阅才能解锁链接。我在下面附上了我的代码。当我再次将相同的代码添加到我的帖子时出现问题,我的频道的 youtube 链接实际上应该在新标签中打开,而不是在新标签中打开,而它在同一个标​​签中打开!这不允许我的观众在帖子关闭和 youtube 打开时解锁内容。此外,第一个 yt 内容储物柜不会出现此问题,而只会出现在第二个内容储物柜中并继续存在。请帮我解决这个问题。此外,我不是任何优秀的编码员,也不是任何关于编码的知识。所以请简化答案。下面我附上了我的 YouTube 内容储物柜代码。

<meta charset="utf-8"></meta>
        <style>
        @import "https://use.fontawesome.com/releases/v5.0.10/css/all.css";
        html, body {
    font-family: "Trebuchet MS", Helvetica, sans-serif;
    }
        #sociallocker {
            background-color: #EEEEEE;
            text-align: center;
            position: relative;
            max-width: 500px;
            height: 120px;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            border-radius:10px;
        }
        #sociallocker-overlay {
            background-color: rgba(0,0,0,0.6);
            font-size: 20px;
            font-weight: bold;
            color: #ffffff;
            transition: all 0.2s ease;
        }
        #sociallocker-overlay i {
            margin-right: 10px;
        }
        #sociallocker:hover #sociallocker-overlay {
            top: -100%;
            transition: all 0.2s linear;
        }
        #sociallocker:hover #sociallocker-content {
            top: 100%;
            transition: all 0.2s linear;
        }
        #sociallocker-content a {
            display: inline-block;
            text-decoration: none;
            padding: 10px 20px;
            background-color: #777777;
            color: #f9f9f9;
            border-radius: 4px;
            font-weight: bold;
        }
        #sociallocker-overlay,
        #sociallocker-content,
        #sociallocker-links{
            position: absolute;
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            top: 0;
            left: 0;
        }
        #sociallocker-content {
            background-color: #ccc;
            transition: all 0.2s ease;
        }
        .social-1 {
            text-decoration: none;
            color: #ffffff;
            display: inline-block;
            width: 498px;
            height: 118px;
            overflow: hidden;
            margin-right: 0px;
        }
        .social-1 i {
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100%;
        }
        .social-1:hover i {
            background-color: rgba(0,0,0,0.1);
            transform: scale(1.2);
            transition: all 0.2s;
        }
        .fb { background-color: #FF0000; }
</style>
        <div id="sociallocker">
            <div id="sociallocker-links">
                <a class="social-1 fb" href="https://www.youtube.com/channel/UCqUX2UrWUz778zSFxPtVRyw?sub_confirmation=1"><i class="fab fa-youtube fa-3x"></i></a>
            </div>
            <div id="sociallocker-content">
                <a href="DOWNLOAD LINK HERE" rel="nofollow" target="_blank"><i class="fas fa-download"></i> Download</a>
            </div>
            <div id="sociallocker-overlay"><i class="fas fa-lock"></i>SUBSCRIBE TO UNLOCK</div>
        </div>
        <script>
        (function() {
            var sl = document.querySelector("#sociallocker");
            var slc = document.querySelector("#sociallocker-content");
            if (!sl) return;
            var old_slc_html = slc.innerHTML;
            slc.innerHTML = slc.innerHTML.replace(/(href=")(.*)(\")/g, "href=\"#\"");
            sl.querySelectorAll("#sociallocker-links a").forEach(function(ele) {
                ele.onclick = function(e) {
                    var web_window = window.open(this.href, 'Share Link', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600,top=' + (screen.height/2 - 300) + ',left=' + (screen.width/2 - 300));
                    var check_window_close = setInterval(function() { 
                        if (web_window.closed) {
                            clearInterval(check_window_close);
                            slc.innerHTML = old_slc_html;
                            document.querySelector("#sociallocker-links").style.display = "none";
                            document.querySelector("#sociallocker-overlay").style.display = "none"; 
                            document.querySelector("#sociallocker-content").style.top = "0";
                        }
                    }, 1000);
                    e.preventDefault();
                };
            });
        })();
        </script>

【问题讨论】:

  • 对我来说它在新窗口中打开很好jsfiddle.net/vanowm/5gvb7caL
  • 嘿,我的意思是,如果我在帖子中只使用此内容锁定一次,它将在新标签页中打开。假设我在博客的另外两个地方添加了相同的内容储物柜。现在,当我单击第一个内容储物柜时,YouTube 订阅会在新的弹出标签中正确打开。但是当我点击我在同一篇文章中添加的第二个和第三个内容储物柜时,YouTube 订阅会在同一页面中打开,因此不会关闭该人正在查看的页面。因此他/她根本无法解锁内容。希望我已经清楚地解释了我的问题。
  • 非常感谢!它就像一个魅力。虽然我不完全了解问题是什么以及您是如何解决的,但我只是知道有两个相同的 ID 发生冲突。无论如何,非常感谢!

标签: javascript html blogger


【解决方案1】:

问题是储物柜使用idid 必须是唯一的,但是当您创建储物柜的多个副本时,这些id 会发生冲突。因此,储物柜的 javascript 部分仅在第一个储物柜上设置事件侦听器。

要解决这个问题,您需要将所有 id 及其引用转换为 css 类,而不是使用单个 javascript:

https://jsfiddle.net/vanowm/5gvb7caL/

<meta charset="utf-8"></meta>
        <style>
        @import "https://use.fontawesome.com/releases/v5.0.10/css/all.css";
        html, body {
    font-family: "Trebuchet MS", Helvetica, sans-serif;
    }
        .sociallocker {
            background-color: #EEEEEE;
            text-align: center;
            position: relative;
            max-width: 500px;
            height: 120px;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            border-radius:10px;
        }
        .sociallocker-overlay {
            background-color: rgba(0,0,0,0.6);
            font-size: 20px;
            font-weight: bold;
            color: #ffffff;
            transition: all 0.2s ease;
        }
        .sociallocker-overlay i {
            margin-right: 10px;
        }
        .sociallocker:hover .sociallocker-overlay {
            top: -100%;
            transition: all 0.2s linear;
        }
        .sociallocker:hover .sociallocker-content {
            top: 100%;
            transition: all 0.2s linear;
        }
        .sociallocker-content a {
            display: inline-block;
            text-decoration: none;
            padding: 10px 20px;
            background-color: #777777;
            color: #f9f9f9;
            border-radius: 4px;
            font-weight: bold;
        }
        .sociallocker-overlay,
        .sociallocker-content,
        .sociallocker-links{
            position: absolute;
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            top: 0;
            left: 0;
        }
        .sociallocker-content {
            background-color: #ccc;
            transition: all 0.2s ease;
        }
        .social-1 {
            text-decoration: none;
            color: #ffffff;
            display: inline-block;
            width: 498px;
            height: 118px;
            overflow: hidden;
            margin-right: 0px;
        }
        .social-1 i {
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100%;
        }
        .social-1:hover i {
            background-color: rgba(0,0,0,0.1);
            transform: scale(1.2);
            transition: all 0.2s;
        }
        .fb { background-color: #FF0000; }
</style>
<!-- first locker !-->
        <div class="sociallocker">
            <div class="sociallocker-links">
                <a class="social-1 fb" href="https://www.youtube.com/channel/UCqUX2UrWUz778zSFxPtVRyw?sub_confirmation=1"><i class="fab fa-youtube fa-3x"></i></a>
            </div>
            <div class="sociallocker-content">
                <a href="DOWNLOAD LINK HERE" rel="nofollow" target="_blank"><i class="fas fa-download"></i> Download</a>
            </div>
            <div class="sociallocker-overlay"><i class="fas fa-lock"></i>SUBSCRIBE TO UNLOCK</div>
        </div>

<!-- second locker !-->
        <div class="sociallocker">
            <div class="sociallocker-links">
                <a class="social-1 fb" href="https://www.youtube.com/channel/UCqUX2UrWUz778zSFxPtVRyw?sub_confirmation=1"><i class="fab fa-youtube fa-3x"></i></a>
            </div>
            <div class="sociallocker-content">
                <a href="DOWNLOAD LINK HERE" rel="nofollow" target="_blank"><i class="fas fa-download"></i> Download</a>
            </div>
            <div class="sociallocker-overlay"><i class="fas fa-lock"></i>SUBSCRIBE TO UNLOCK</div>
        </div>

<!-- third locker !-->
        <div class="sociallocker">
            <div class="sociallocker-links">
                <a class="social-1 fb" href="https://www.youtube.com/channel/UCqUX2UrWUz778zSFxPtVRyw?sub_confirmation=1"><i class="fab fa-youtube fa-3x"></i></a>
            </div>
            <div class="sociallocker-content">
                <a href="DOWNLOAD LINK HERE" rel="nofollow" target="_blank"><i class="fas fa-download"></i> Download</a>
            </div>
            <div class="sociallocker-overlay"><i class="fas fa-lock"></i>SUBSCRIBE TO UNLOCK</div>
        </div>
<!-- end locker !-->

        <script>
        (function() {
            var sls = document.querySelectorAll(".sociallocker");
            sls.forEach(sl =>
            {
              var slc = sl.querySelector(".sociallocker-content");
              if (!sl) return;
              var old_slc_html = slc.innerHTML;
              slc.innerHTML = slc.innerHTML.replace(/(href=")(.*)(\")/g, "href=\"#\"");
              sl.querySelectorAll(".sociallocker-links a").forEach(function(ele) {
                  ele.onclick = function(e) {
                      var web_window = window.open(this.href, 'Share Link', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600,top=' + (screen.height/2 - 300) + ',left=' + (screen.width/2 - 300));
                      var check_window_close = setInterval(function() { 
                          if (web_window.closed) {
                              clearInterval(check_window_close);
                              slc.innerHTML = old_slc_html;
                              sl.querySelector(".sociallocker-links").style.display = "none";
                              sl.querySelector(".sociallocker-overlay").style.display = "none"; 
                              sl.querySelector(".sociallocker-content").style.top = "0";
                          }
                      }, 1000);
                      e.preventDefault();
                  };
              });
            });
        })();
        </script>

【讨论】:

  • 嘿,我的意思是,如果我在帖子中只使用此内容锁定一次,它将在新标签页中打开。假设我在博客的另外两个地方添加了相同的内容储物柜。现在,当我单击第一个内容储物柜时,YouTube 订阅会在新的弹出标签中正确打开。但是当我点击我在同一篇文章中添加的第二个和第三个内容储物柜时,YouTube 订阅会在同一页面中打开,因此不会关闭该人正在查看的页面。因此他/她根本无法解锁内容。希望我已经清楚地解释了我的问题。
  • @KnightPhotoshop 我已经更新了我的答案和代码
【解决方案2】:

我并不确定我理解你的意思,但我理解的是你说它在同一个标​​签页中打开并且你希望它在不同的标签页中打开。 window.open(this.href, 'Share Link'... 必须是window.open(this.href, '_blank'...,否则会在同一个标​​签页中打开。该函数的第二个参数(“共享链接”或“_blank”)指定窗口名称,但如果将“_blank”传递给它,它会告诉窗口在新选项卡中打开。

【讨论】:

  • 实际上,它将在一个新选项卡中打开,并会在下一个弹出窗口中再次使用该选项卡。如果该选项卡关闭,它将再次在新选项卡中打开。
  • 我不确定你在说什么,这不是你想要的,让它在新标签页中打开吗?
  • 是的,但在某些情况下,最好重复使用以前打开的选项卡。也就是说,如果您有不同的链接到不同的搜索引擎,那么它会在 tab1 中打开所有指向 google 的链接,并在 tab2 中打开指向 bing 的链接,而不是打开 10 个不同的选项卡。因此,在 OPs 的情况下,最好将之前打开的标签重新用于下一次“订阅”点击,而不是打开 2 个不同的标签。
  • 嘿,你们能说得更具体些吗?正如我所说,我什至不知道代码和 html 的 abcd
  • 我已经尝试了你所有的方法,但似乎没有一个有效。我在下面添加了我的问题的更多详细信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-26
  • 2021-10-17
  • 2018-07-22
相关资源
最近更新 更多