【问题标题】:Open link to specific domain within an iFrame in a new window在新窗口中打开指向 iFrame 中特定域的链接
【发布时间】:2011-02-27 20:16:13
【问题描述】:

我需要能够打开指向 iFrame 中特定 URL 的链接;链接是php生成的,我无法更改源代码,所以我需要动态更改iFrame中的链接,如果可能的话。

我正在做的是:在一个非营利组织的网站上,我有一个附属商店(由 php 生成)显示在页面的 iFrame 中,以使其与网站的其余部分更具风格。 php 商店从附属服务中提取产品;指向 store.php 的链接与主要非盈利网站位于同一域中。

问题是产品的最终“立即购买”链接在 iFrame 中打开了最终目的地电子商务商店,因此页面卡在 iFrame 中并且看起来很糟糕,必须滚动 V 和 H 才能看到看看吧。

我需要在新窗口中打开的链接始终具有类“av_buy_now”我可以访问 CSS 文件,我可以更改它。我可以访问 PHP 文件,即 shop.php,但最终链接似乎不是在本地生成的。我以这种方式显示 iframe:

<iframe src="http://nonprofit.org/shop/mj/shop.php"></iframe>

是否可以使用 jQuery 或 Javascript 来查找带有该类的 mydomain.com 的链接并向它们添加新的窗口属性?或者,还有更好的方法?谢谢

【问题讨论】:

    标签: javascript jquery iframe


    【解决方案1】:

    如果没有一些代码,我不知道您的代码的确切结构,但我可以完成这项工作。它也要求 jQuery 在 iframe 中。

    $('iframe')[0].$('.av_buy_now').filter(function() {
        return $(this).attr('href').indexOf('mydomain.com') > -1;
    }).attr('target', '_blank');
    

    【讨论】:

    • 嗯.. 我如何在 iFrame 中包含 jQuery?我可以将它包含在 iFrame 显示的 html 模板中;是这样吗?
    【解决方案2】:

    如果你能设法执行这种代码,你可以改变它,改变类有点麻烦,你有一个id就可以做这样的事情。

    PAGE.html:

    <button onclick="openinnewwin()">Get It</button>
    <iframe src="test.html" id="ifrm"></iframe>
    <script language="javascript">
    function openinnewwin(){
    ob=document.getElementById('ifrm');
    if ( ob.contentDocument ) {
        ob.contentDocument.getElementById('a').target="_blank"; 
    }else if ( ob.contentWindow ){
        ob.contentWindow.getElementById('a').target="_blank";   
    }
    }
    </script>
    

    test.html:

    <a href="http://www.google.com" class="aaa" id="a">Hello</a>
    

    【讨论】:

      猜你喜欢
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-21
      相关资源
      最近更新 更多