简体   繁体   English

向 Javascript Iframe 弹出窗口添加多个链接

[英]Add multiple links to Javascript Iframe popup

how do I add multiple links to this JavaScript, the JavaScript is an Iframe popup, triggered by an external link, I need to add 3 links to trigger 3 different page popups.如何向此 JavaScript 添加多个链接,JavaScript 是一个 Iframe 弹出窗口,由外部链接触发,我需要添加 3 个链接以触发 3 个不同的页面弹出窗口。

I have studied the JavaScript and tried different ways.I searched stack, and found nothing that could provide a solution.我研究了 JavaScript 并尝试了不同的方法。我搜索了堆栈,没有找到任何可以提供解决方案的方法。

Any help would be appreciated.任何帮助,将不胜感激。

Here is the HTML for using the JavaScript with 1 link.这是使用带有 1 个链接的 JavaScript 的 HTML。

 document.getElementById("link").onclick = function(e) { e.preventDefault(); document.getElementById("popupdarkbg").style.display = "block"; document.getElementById("popup").style.display = "block"; document.getElementById('popupiframe').src = "http://example.com"; document.getElementById('popupdarkbg').onclick = function() { document.getElementById("popup").style.display = "none"; document.getElementById("popupdarkbg").style.display = "none"; }; return false; } window.onkeydown = function(e) { if (e.keyCode == 27) { document.getElementById("popup").style.display = "none"; document.getElementById("popupdarkbg").style.display = "none"; e.preventDefault(); return; } }
 #popup { display: none; position: fixed; top: 12%; left: 15%; width: 70%; height: 70%; background-color: transparent; z-index: 10; } #popup iframe { width: 100%; height: 100%; border: 0; } #popupdarkbg { position: fixed; z-index: 5; left: 0; top: 0; width: 100%; height: 100%; overflow: hidden; background-color: rgba(0,0,0,.75); display: none; }
 <div id="main"> <a href="" id="link">Click me</a><br> </div> <div id="popup"><iframe id="popupiframe"></iframe></div> <div id="popupdarkbg"></div>

You can use class to refer multiple elements.您可以使用class来引用多个元素。 Select all the elements with the class with querySelectorAll() , then loop through them to attach the event.使用querySelectorAll()选择具有该类的所有元素,然后遍历它们以附加事件。

You can associate the related link in the a element itself using a custom attribute, then on click you can retrieve that and set that as the popup iframe src .您可以使用自定义属性关联a元素本身中的相关链接,然后单击您可以检索该链接并将其设置为弹出 iframe src

Try the following way:尝试以下方式:

 document.querySelectorAll('.link').forEach(function(lk){ lk.onclick = function(e) { e.preventDefault(); document.getElementById("popupdarkbg").style.display = "block"; document.getElementById("popup").style.display = "block"; document.getElementById('popupiframe').src = this.getAttribute('data-link'); console.log(this.getAttribute('data-link')); document.getElementById('popupdarkbg').onclick = function() { document.getElementById("popup").style.display = "none"; document.getElementById("popupdarkbg").style.display = "none"; }; return false; } }); window.onkeydown = function(e) { if (e.keyCode == 27) { document.getElementById("popup").style.display = "none"; document.getElementById("popupdarkbg").style.display = "none"; e.preventDefault(); return; } }
 #popup { display: none; position: fixed; top: 12%; left: 15%; width: 70%; height: 70%; background-color: transparent; z-index: 10; } #popup iframe { width: 100%; height: 100%; border: 0; } #popupdarkbg { position: fixed; z-index: 5; left: 0; top: 0; width: 100%; height: 100%; overflow: hidden; background-color: rgba(0,0,0,.75); display: none; }
 <div> <a href="" class="link" data-link="http://example.com">Click me</a><br> <a href="" class="link" data-link="http://example-2.com">Click me 2</a> </div> <div id="popup"><iframe id="popupiframe"></iframe></div> <div id="popupdarkbg"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM