从新打开的窗口jquery中删除节点

时间:2013-07-30 07:30:09

标签: javascript jquery dom

我正在尝试使用以下代码打开一个新窗口。

$("#printBtn").on("click", function () {
     var w = window.open(this.href, "myWindowName", "width=800, height=600");
     $(w.document.body).children(".top-fixed-nav").remove();
     return false;
});

我遇到的问题是新窗口打开时需要输出但我使用$(w.document.body).children(".top-fixed-nav").remove();的行无法正常工作,即.top-fixed-nav不会删除。我已经尝试将它绑定到ready事件

$("#printBtn").on("click", function () {
       var w = window.open(this.href, "myWindowName", "width=800, height=600");
    $(w).ready(function(){
        $(w.document.body).children(".top-fixed-nav").remove();
    });
    return false;
});

但这也不起作用。谁能告诉我,我做错了什么?

更新

试过这个:

$("#printBtn").on("click", function () {
       var w = window.open(this.href, "myWindowName", "width=800, height=600");
//        $(w.document).ready(function(){
// and    $(w.document).load(function(){
        $(w.document.body).children(".top-fixed-nav").remove();
    });
    return false;
});

这两个都不起作用。

3 个答案:

答案 0 :(得分:3)

$("#printBtn").on("click", function () {
    var w = window.open(this.href, "myWindowName", "width=800, height=600");
    $(w).on("load", function(){
        $(w.document.body).children(".top-fixed-nav").remove();
    });
    return false;
});

尝试这个,因为onload方法适用于窗口而不是文档。

答案 1 :(得分:2)

尝试绑定加载而不是就绪:

$("#printBtn").on("click", function () {
    var w = window.open(this.href, "myWindowName", "width=800, height=600");
    $(w.document).on("load", function(){
        $(w.document.body).children(".top-fixed-nav").remove();
    });
    return false;
});

经过一番摆弄后得到了这个:

$("#printBtn").on("click", function () {
    var w = window.open(this.href, "myWindowName", "width=800, height=600");
    var callInterval = setInterval(childCall, 100);
    function childCall(){
        if (typeof w.jQuery !== "undefined") {
            //w.jQuery(document.body).children(".top-fixed-nav").remove();
            w.jQuery(".top-fixed-nav").remove();
            if(typeof callInterval !== "undefined")
                window.clearInterval(callInterval);
        }

    };
    return false;
});

尝试一下,让我们知道它是否有效:D

答案 2 :(得分:0)

你可以试试这个:

var w = window.open(this.href, "myWindowName", "width=800, height=600");
w.document.$('body').children(".top-fixed-nav").remove();

可替换地:

$(".top-fixed-nav", w.document.body).remove();

注意:您可能需要引入延迟才能加载窗口。

setTimeout('$(".top-fixed-nav", w.document.body).remove()', 5000);