javascript iframe 窗口中的链接以在新选项卡中打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17128889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
links within an iframe window to open in new tab
提问by doc holiday
I have seen this question many times, and a lot of the answers seem to suggest the base target="_blank"
technique. However, I have used this before in the past; but my current page it does not work. I also don't think it could be best option even if it did work; as I ONLY want the links within the iframe src=""
to open in a new window. I am hopping there's a simple solution I can add inline to the page. I have also tried adding an id as below, and using JavaScript, still nada.
我已经多次看到这个问题,很多答案似乎都暗示了基本target="_blank"
技术。但是,我过去曾经使用过它;但我当前的页面不起作用。我也不认为它可能是最好的选择,即使它确实有效;因为我只希望 中的链接在iframe src=""
新窗口中打开。我在跳有一个简单的解决方案,我可以将内联添加到页面中。我也试过添加一个 id 如下,并使用 JavaScript,仍然 nada。
<iframe src="mywordpressfeed.html" id="frame1" width="310" height="380"></iframe>
JS
JS
$(document).ready(function(){
$("#frame1").attr("target","_blank");
});
Basically the goal is to when a user sees my wordpress feed within the iframe I have on a static page; once the post title is clicked it loads in a new window - as now it loads within the same iframe so there isn't an increased level of readability.
基本上目标是当用户在静态页面上的 iframe 中看到我的 wordpress 提要时;单击帖子标题后,它会在新窗口中加载 - 因为现在它会在同一个 iframe 中加载,因此不会提高可读性。
回答by doc holiday
There is no real solution to this, due to the iFrame tag being developed for the opposite.
对此没有真正的解决方案,因为 iFrame 标签是为相反的方向开发的。
回答by raam86
//pass the iframe to this iframe getting function
function iframeRef( frameRef ) {
return frameRef.contentWindow ? frameRef.contentWindow.document : frameRef.contentDocument
}
//Get Iframe
var inside = iframeRef( document.getElementById('iframeID') );
//Get all links
var links = inside.getElementsByTagName('a');
//Loop throught links and set their attributes
for (var i = 0 ; i<links.length ; i++){
links[i].setAttribute('target','_blank');
}
//No jQuery needed!
thanks to meder
感谢meder
EDITDue to iframe same source restrictions I had to find a website with inner iframe from same source so you can paste this code
编辑由于 iframe 相同的源限制,我必须从相同的源中找到一个带有内部 iframe 的网站,以便您可以粘贴此代码
//pass the iframe to this iframe getting function
function iframeRef( frameRef ) {
return frameRef.contentWindow ? frameRef.contentWindow.document : frameRef.contentDocument
}
//Get Iframe
var inside = iframeRef( document.getElementById('IFwinEdit_Gadget_247730_3349') );
//Get all links
var links = inside.getElementsByTagName('input');
//Loop throught links and set their attributes
for (var i = 0 ; i<links.length ; i++){
links[i].setAttribute('style','background:red');
}
//No jQuery needed!
to the console in this web siteand see the inputs change color
到此网站中的控制台并查看输入更改颜色