【问题标题】:How to make a link open in tabbed or new window without target attribute?如何在没有目标属性的选项卡式或新窗口中打开链接?
【发布时间】:2011-01-15 15:40:57
【问题描述】:

在我的应用程序中,一个外部网站在我的网页中放置了链接,而该链接没有 目标=_空白。更具体地说,它在我的网页中的 iframe 其 src 是我无法控制的另一个域。此 iframe 中的内容包含在同一窗口中打开的链接,这些链接会擦除我的 ajax 驱动网页并将用户带到另一个域。有没有办法拦截这个(通过javascript?)并使链接在另一个窗口或浏览器选项卡中打开?如果链接在我网页上的同一个 iframe 中打开,那就更好了。这可能吗? target="_self" 是否使链接在同一个 iframe 中打开?

【问题讨论】:

    标签: javascript html iframe


    【解决方案1】:

    您可以将目标属性添加到 iframe 中的所有 a-tags。这可以在 jQuery 中完成,例如:

    $("a", ".myIframe").attr("target", "_blank");
    

    这只是伪代码。请参阅此处,如何访问 iframe 中的元素:

    jQuery/JavaScript: accessing contents of an iframe

    【讨论】:

    • 这不适用于不同域上的 IFrame - 请参阅 same origin policy
    • 这不是解决方案。请注意问题:iframe 内容来自不同的域,我无法控制或访问在那里安装任何东西。
    【解决方案2】:

    ** 使用 target 属性在新标签页中打开一个 url

    <a target="_blank" href="http://www.google.com" title="">
    Click here for new tab
    </a>
    

    ** 在没有目标属性的新标签页中打开 url。

    <a onclick="window.open(this.href,'')" 
    href="http://www.google.com"  title="">
    Click here for new tab withouth target attribute</a>
    

    ** 在同一个框架中打开一个url。

    <a target="_self" href="https://jsfiddle.net/" title="">
    Click here to open in the same frame</a>
    

    请找到运行良好的 jsfiddle
    jsfiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-11
      • 2010-09-12
      • 1970-01-01
      • 2010-12-01
      • 2013-03-22
      • 2011-05-09
      • 2012-01-22
      相关资源
      最近更新 更多