繁体   English   中英

使用按钮关闭iframe模式

[英]Close iframe modal with button

我正在使用以下内容从小书签加载iframe:

javascript:(function(d){
var%20modal=document.createElement('iframe');
modal.setAttribute('src','http://myurl.com/page.html?url=
'+encodeURIComponent(window.location.href)+'&
page_title='+document.title);
modal.setAttribute('scrolling','no');
modal.className='modal';
document.body.appendChild(modal);
var c=document.createElement('link');
c.type='text/css';
c.rel='stylesheet';
c.href='//myurl.com/css/wl_iframe.css';
document.body.appendChild(c);
}(document));

并关闭它,我正在尝试:

<button onclick="parent.$.iframe.close();">X Close Window</button>

我也尝试过:

<button onclick="modal.$.iframe.close();">X Close Window</button>

但是他们没有工作,他们什么也没做。

我知道我可以使用:

<a href="underneath url" target="_parent">X Close Window</a>

但这然后导致浏览器重新刷新他们正在查看的页面,并且用户还必须按两次后退按钮才能进入该页面,在此之前并不理想。

有任何想法吗?

您应该在iframe中添加一些名称或ID

javascript:(function(d){
var modal=document.createElement('iframe');
modal.setAttribute('src','http://myurl.com/page.html?url='+encodeURIComponent(window.location.href)+'&page_title='+document.title);
modal.setAttribute('scrolling','no');

modal.setAttribute('name', 'my_modal_window');

modal.className='modal';
document.body.appendChild(modal);
var c=document.createElement('link');
c.type='text/css';
c.rel='stylesheet';
c.href='//myurl.com/css/wl_iframe.css';
document.body.appendChild(c);
}(document));

接着

<a href="#" onclick="var modals = document.getElementsByName('my_modal_window'), i = modals.length; while(i--){ modals[i].parentNode.removeChild(modals[i])}">X close window</a>

EDIT工作解决方案,刚刚在Chrome JS控制台内部对stackoverflow进行了测试:

(function(d){
var modal=document.createElement('iframe');
modal.setAttribute('src','http://myurl.com/page.html?url='+encodeURIComponent(window.location.href)+'&page_title='+document.title);
modal.setAttribute('scrolling','no');

modal.setAttribute('name', 'my_modal_window');

modal.className='modal';
document.body.appendChild(modal);
var c=document.createElement('link');
c.type='text/css';
c.rel='stylesheet';
c.href='/css/wl_iframe.css';
document.body.appendChild(c);

var a = document.createElement('a');
a.onclick= function(){
   var modals = document.getElementsByName('my_modal_window'),i = modals.length;
   while (i--) {
      modals[i].parentNode.removeChild(modals[i]);
   }
};
a.innerHTML = 'Close Iframes';
document.body.appendChild(a);
}(document))

暂无
暂无

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

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