【问题标题】:Scope changes during HTML parsingHTML 解析期间的范围更改
【发布时间】:2016-11-12 19:21:41
【问题描述】:

如果您像这样在 HTML 中创建表单:

<!DOCTYPE html>
<html><head>
<script>
function submit() {
    console.log("window.submit");
}
</script>
</head>
<body>
<form name="form">
    <input type="button" value="button" onclick="submit()" />
</form>
</body>
</html>

input标签被解析时(至少在chrome中),相应的DOM元素显然会以表单为范围创建,因此绑定到onclick处理程序的函数将是form.submit()而不是@ 987654325@。这是标准行为还是取决于浏览器?是否有任何文档涵盖了这一点?

【问题讨论】:

标签: html dom scope


【解决方案1】:

WHATWG HTML 标准在事件处理程序属性中定义它 https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-attributes

范围

    - If H is an element's event handler, then let Scope 
    be NewObjectEnvironment(document, the global environment).
    - Otherwise, H is a Window object's event handler: let Scope be the global environment.
    - If form owner is not null, let Scope be NewObjectEnvironment(form owner, Scope).
    - If element is not null, let Scope be NewObjectEnvironment(element, Scope).

在这种情况下,由于表单所有者不为空,因此表单的每个属性都将在范围内。 “submit”是form的一个属性,所以“submit()”会调用form.submit()。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-20
  • 2015-08-24
  • 2015-11-29
  • 1970-01-01
  • 1970-01-01
  • 2019-09-15
  • 1970-01-01
相关资源
最近更新 更多