2017-08-30 28 views
0

我想要删除要在包含刷新的iframe的HTML文件上显示的所有图像。
是否有可能使用CSS或JavaScript的方法?使用CSS或JavaScript从iframe中的网页中删除所有图像

加载iframe的页面使用下面的代码。

function reloadIFrame() { 
 
    console.log('reloading..'); 
 
    document.getElementById('iframe').contentWindow.location.reload(); 
 
} 
 

 
window.setInterval(reloadIFrame, 3000);
body { 
 
    margin: 0px; 
 
} 
 
h1 { 
 
    font-family: arial; 
 
    font-size: 50%; 
 
} 
 
iframe { 
 
    width: 100%; 
 
    height: 800px; 
 
} 
 
div.top { 
 
    background-color: rgba(2, 109, 8, 0.83); 
 
    height: 1%; 
 
    width: 100%; 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <link rel="shortcut icon" href="news.png" type="image/x-icon" /> 
 
    <title>Chat</title> 
 
</head> 
 
<body> 
 
    <div class="top"> 
 
    <img src="news.png" alt="newsicons" height="31" width="31"> 
 
    <h1>News</h1> 
 
    </div> 
 

 
    <iframe id="iframe" src="http://www.bbc.com/news/world"></iframe> 
 
</body> 
 
</html>

回答

0

您可以用javascript做到这一点。检查下面更新片断..

if(document.getElementById('iframe')) { 
 
    var images = document.getElementsByTagName('img'); 
 
    for (i = 0; i < images.length;i++) { 
 
    images[i].style.display = "none"; 
 
    } 
 
}
<div class="top"> 
 
     <img src="news.png" alt="newsicons" height="31" width="31"> 
 
     <h1>News</h1> 
 
    </div> 
 

 
    <iframe id="iframe" src="http://www.bbc.com/news/world"></iframe>

+0

我觉得这可能无法正常工作,由于跨域政策。如果iframe源在同一个域上,它应该可以工作。 – nivincp

+0

相同的来源策略不会对此代码产生任何影响:它不会尝试访问框架中的文档。 – Quentin

+0

@Quentin true,但假设它将访问iframe html内容.. :-) – nivincp

相关问题