【发布时间】:2018-01-25 03:58:22
【问题描述】:
在 HTML 中使用事件处理程序内容属性时,根据specification,回调中this 的值是指事件的currentTarget 属性(下例中的input DOM 元素)。要访问 DOM 元素的属性之一,可以这样做:
<input type="text" onclick="console.log(this.type)"><!-- logs "text" -->
但是,当我在另一个问题上写answer 时,我有点意外地发现可以使用元素的非限定属性名称来引用它们的属性。以下内容已在当前版本的 Chrome 和 Firefox 中进行了测试:
<input type="text" onclick="console.log(type)"><!-- logs "text" -->
<input type="text" onclick="console.log(required)"><!-- logs "false" -->
<input type="text" onclick="console.log(tagName)"><!-- logs "INPUT" -->
现在,我的问题是:这种行为是否在某处指定?我似乎没有在 HTML specification 或 DOM UI events specification 中找到任何内容,而且我的 Google 搜索结果也是空的。
【问题讨论】:
标签: javascript html event-handling dom-events