1

我注意到 javascript 中的“with”关键字以及父子窗口关系,特别是 window.opener 的一些特殊之处。我没有从父窗口测试过这个,只是孩子,但在下面的例子中值得注意 -

父窗口(Parent.html):

// global scope
function ParentFunc() { alert("I am a parent function"); }

子窗口(Child.html):

// global scope
var baseWin = window.opener;

// "this" keyword corresponds to Child.html when function below is called
function Child_Onclick_Func()
{
  alert("Hi, from Child");
  with (baseWin)
  {
    ParentFunc();
    alert("Hi, from Parent");
  }
  alert("Hi, back to Child");
}

在这种情况下,“with”关键字切换到父窗口,第二个警报也将隐式 onfocus 触发到父窗口。我没有意识到“with”会切换到父窗口,但现在它是有道理的。

4

1 回答 1

1

发生这种情况是因为window在 Web 浏览器中运行 javascript 时是全局命名空间。当你写:

alert('Hello, World!');

您实际上是在调用该window.alert方法。

于 2011-06-16T15:33:01.077 回答