我注意到 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”会切换到父窗口,但现在它是有道理的。