javascript - chrome 71 不会检测 onclick 事件中的 id,除非它是 this.id

标签 javascript google-chrome

我有一个像这样的 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 的错误还是……功能?

提前谢谢您。

编辑:这种情况发生在 selecta 标记上,但不会发生在 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/

相关文章:

javascript - 如何找到大图像各部分的坐标来创建图像映射?

javascript - '$ ("#elementId").change( )' is not working and ' $(document).on ("change" "elementId")' 正在工作

javascript - 一些具有隐藏值的数据列表标签

javascript - 用于 nodejs 自动完成的 Ternjs

javascript - 调试 Chrome 原生消息传递

javascript - Chrome 扩展将 Background.js 中的值保存到存储中并显示在 popup.html 上

php - 在移动浏览器中显示使用 mpdf inline 生成的 pdf

javascript - 使用 redux react 原生 index.js

javascript - Google Charts ColumnChart 硬最小轴值溢出标签(仅限 Chrome)

javascript - Chrome 内容脚本