【问题标题】:javascript open window referencesjavascript 打开窗口参考
【发布时间】:2010-03-22 15:53:45
【问题描述】:

我在理解打开新浏览器窗口后如何引用它们时遇到了一些问题。例如,如果我从一个主窗口(index.html)创建了 3 个新窗口:

    var one = window.open( 'one.html', 'one',"top=10,left=10,width=100,height=100,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no");

    var two = window.open( 'two.html', 'two',"top=100,left=10,width=100,height=100,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no");

    var three = window.open( 'three.html', 'three',"top=200,left=10,width=100,height=100,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no");
    two.focus();

如果浏览器“二”当前处于焦点位置,我如何以编程方式关注(或仅引用)浏览器“三”?

【问题讨论】:

    标签: javascript browser window


    【解决方案1】:

    我会在父窗口中有一组子窗口。然后,对于每个子窗口,都有一个函数将子窗口添加到父窗口的 childWindow 数组中。这样您就可以拥有任意数量的子窗口。

    //In the 'Main' window
    var childWindows = new Array();
    
    //In the child window
    function onload()
    {
        window.parent.childWindows.push(window);
    
    }
    
    window.attachEvent('onload', onload);
    //or
    window.load = onload
    //or with jQuery
    $(window).ready(onload)
    

    像这样设置焦点:

    //In the parent
    
    childwindows[i].focus();
    
    //In a child
    
    parent.childwindows[i].focus();
    
    //or to focus on next child from within a child
    var len = parent.childwindows.length;
    for (var i = 0; i < len; i++)
    {
        if (parent.childwindows[i] && parent.childwindows[i] == window) //you might want some other way to determine equality e.g. checking the title or location property.
        {
            var n = (i + 1) % childWindows.length;
            childwindows[n].focus();
        }
    
    }
    

    【讨论】:

    • 对于“window.parent.childWindows.push(window);”即使我在父窗口中声明了 childWindows 数组,我仍然收到“无法调用未定义的方法‘push’”
    • 试试window.opener.childWindows
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    相关资源
    最近更新 更多