【问题标题】:links within an iframe window to open in new tabiframe 窗口中的链接以在新选项卡中打开
【发布时间】:2013-06-12 07:22:22
【问题描述】:

我已经多次看到这个问题,很多答案似乎都暗示了基本的target="_blank" 技术。但是,我过去曾使用过它;但我当前的页面不起作用。我也不认为它可能是最好的选择,即使它确实有效。因为我只希望iframe src="" 中的链接在新窗口中打开。我希望有一个简单的解决方案可以将内联添加到页面中。我也尝试过添加如下的 id,并使用 JavaScript,仍然没有。

<iframe src="mywordpressfeed.html" id="frame1" width="310" height="380"></iframe> 

JS

$(document).ready(function(){
    $("#frame1").attr("target","_blank");
});

基本上,目标是当用户在静态页面上的 iframe 中看到我的 wordpress 提要时;单击帖子标题后,它会在新窗口中加载 - 现在它会在同一个 iframe 中加载,因此不会提高可读性。

【问题讨论】:

  • "target" 是锚标记的属性,而不是 iframe 标记。
  • 你不能编辑你的 Wordpress 主题/代码/提要给超链接 target="_blank" 选项吗?
  • 感谢@adaam 的回复,但不,因为当用户在那里时,我不希望它成为每次点击后的新页面。仅在我拥有 iframe 的初始主页上。

标签: javascript jquery html


【解决方案1】:

由于 iFrame 标签是针对相反的情况开发的,因此没有真正的解决方案。

【讨论】:

    【解决方案2】:
    //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!
    

    感谢meder

    编辑 由于 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!
    

    web site 中的控制台,查看输入的颜色变化

    【讨论】:

    • 哇,这看起来棒极了@raam86 但仍然没有这样的运气 =/
    • 还有什么我应该在这里包括的吗?干杯。
    • 发布一些 HTML 以获得更多帮助,同时粘贴控制台错误
    • 再次感谢您,@raam86 你就是那个人。唯一相关的 HTML 是原始帖子中的 iframe 标记。但是,我收到一个控制台错误,两个代码都说明“错误:document.getElementById(...)”为空。
    • 你使用 frame1 作为 frameref 吗?还要注意 crosd 网站限制
    猜你喜欢
    • 2020-05-10
    • 2013-03-22
    • 1970-01-01
    • 2012-01-22
    • 2011-02-18
    • 1970-01-01
    • 2013-11-11
    • 2013-03-11
    相关资源
    最近更新 更多