【问题标题】:Load img in window.opener from child window using jQuery使用 jQuery 从子窗口加载 window.opener 中的 img
【发布时间】:2012-01-11 22:35:48
【问题描述】:

我试图在 window.opener 中从打开的窗口中设置图像,如下所示:

$(document).ready(function(){
    $('a').click(function (event){
        event.preventDefault();
        var linkID =$(this).attr("id");         
        var imgSrcVal = $('img', this).attr("src");

        window.opener.document.getElementById("id_1").src=imgSrcVal;
     });
});

在父窗口中,我的 img 标签看起来像这样:

<tr>
    <td style="height:250px;">
        <img src=""   width="110" height="250px" id="id_1"/>
    </td>
</tr>

可以在不刷新的情况下设置和显示吗?如果不是,我怎么能以 ajax 方式做到这一点?

【问题讨论】:

  • 当您使用您发布的代码时会发生什么?另外,两个窗口内容的来源相同吗?

标签: jquery image load refresh


【解决方案1】:

确保打开器的域和当前窗口相同(Same Origin Policy)。如果这是真的,那么开瓶器和当前寡妇的路径可能不一样。尝试将图像 Url 设为绝对...

$(document).ready(function(){
    $('a').click(function (event){
        event.preventDefault();
        var linkID =$(this).attr("id");         
        var imgSrcVal = $('img', this).attr("src");

        if (location.href.indexOf("/") != 0) {
            var base = location.href.replace(/(.+)(\/)(.*)/, "$1");
            imgSrcVal = base + "/" + imgSrcVal;
        }

        window.opener.document.getElementById("id_1").src = imgSrcVal;
     });
});

【讨论】:

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