簡體   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