我正在测试一段代码。我有一个名为 的函数write
,我使用了一个带有内联onclick
处理程序的按钮来运行write()
。
function write(text) {
alert(text)
}
<button onclick='write("Some text")'>
Write
</button>
令我惊讶的是,write()
居然被执行了document.write()
。可以肯定的是,我用更多的功能对其进行了测试。
function write(text) {
alert(text)
}
<button onclick='console.log(body)'>
document.body
</button>
然后,我想知道他们是否可以访问window
. 事实证明,他们可以。但是,document.window
是undefined
。
console.log(document.window)
/*
Note that this example may not work due to StackSnippets using iframes
*/
<button onclick='window.open()'>
Open blank page
</button>
function doSomething(text) {
console.log(text)
}
<button onclick='doSomething("Some text")'>
doSomething
</button>
所以,我的问题是,为什么内联事件处理程序可以访问的属性window
,即使document
是全局范围,并且document.window
是undefined
?