我有一个像这样的 html 元素:
<select id="myselect" onclick="jsDoSomething(id)"><option>1</option><option>2</option></select>
在 Chrome 70 之前,一切正常,在 Chrome 71 中,jsDoSomething
函数中的 id
为 null。我必须将其更改为 this.id
才能使其正常工作。这是 Chrome 71 的错误还是……功能?
提前谢谢您。
编辑:这种情况发生在 select
和 a
标记上,但不会发生在 button
标记上。
编辑2:jsfiddle:http://jsfiddle.net/5L0djvqy/10/
最佳答案
从这里开始:
https://html.spec.whatwg.org/multipage/webappapis.html#the-event-handler-processing-algorithm
Scope
If eventHandler is an element's event handler, then let Scope be NewObjectEnvironment(document, the global environment).
Otherwise, eventHandler 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).
看来这种行为是标准的(令人惊讶的是,我从来没有这样做过),所以 Chrome 上似乎有些问题。
但是,我在 Linux 上的 Chrome 71 上测试了一个简单的代码,并得到了“预期”的结果。 也许你的 HTML 结构在某处被破坏,并且该元素不是当前的选择框
关于javascript - chrome 71 不会检测 onclick 事件中的 id,除非它是 this.id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53740067/