如何找出触发了哪些 JavaScript 事件?

发布于 2024-09-25 06:05:37 字数 662 浏览 3 评论 0原文

我有一个选择列表:

<select id="filter">
  <option value="Open" selected="selected">Open</option>
  <option value="Closed">Closed</option>
</select>

当我选择关闭时,页面会重新加载。在这种情况下,它显示已关闭的票证(而不是打开的票证)。当我手动执行时效果很好。

问题是,当我使用 Watir 选择 Closed 时,页面不会重新加载:

browser.select_list(:id => "filter").select "Closed"

这通常意味着某些 JavaScript 事件未触发。我可以使用 Watir 触发事件:

browser.select_list(:id => "filter").fire_event "onclick"

但我需要知道要触发哪个事件。

有没有办法找出为元素定义了哪些事件?

I have a select list:

<select id="filter">
  <option value="Open" selected="selected">Open</option>
  <option value="Closed">Closed</option>
</select>

When I select Closed the page reloads. In this case it shows closed tickets (instead of opened). It works fine when I do it manually.

The problem is that the page does not reload when I select Closed with Watir:

browser.select_list(:id => "filter").select "Closed"

That usually means that some JavaScript event is not fired. I can fire events with Watir:

browser.select_list(:id => "filter").fire_event "onclick"

but I need to know which event to fire.

Is there a way to find out which events are defined for an element?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

转瞬即逝 2024-10-02 06:05:37

我想补充一下,您也可以在 Chrome 中执行此操作:

Ctrl + Shift + I (开发人员工具)>来源>事件侦听器断点(右侧)。

您还可以通过右键单击该元素然后浏览其属性(右侧面板)来查看已附加的所有事件。

例如:

  • 右键单击左侧的赞成按钮
  • 选择检查元素
  • 折叠样式部分(最右侧的部分 - 双 V 形)
  • 展开事件侦听器选项
  • 现在您可以看到绑定到赞成票的事件
  • 不确定它是否与firebug 选项,但对于我的大部分东西来说已经足够了。

    另一个有点不同但令人惊讶的很棒的选项是视觉事件:
    http://www.sprymedia.co.uk/article/Visual+Event+2

    它突出显示页面上已绑定的所有元素,并具有显示所调用函数的弹出窗口。非常漂亮的书签!如果您更喜欢的话,还有一个 Chrome 插件 - 不确定其他浏览器是否如此。

    AnonymousAndrew 还指出monitorEvents(window);

    Just thought I'd add that you can do this in Chrome as well:

    Ctrl + Shift + I (Developer Tools) > Sources> Event Listener Breakpoints (on the right).

    You can also view all events that have already been attached by simply right clicking on the element and then browsing its properties (the panel on the right).

    For example:

  • Right click on the upvote button to the left
  • Select inspect element
  • Collapse the styles section (section on the far right - double chevron)
  • Expand the event listeners option
  • Now you can see the events bound to the upvote
  • Not sure if it's quite as powerful as the firebug option, but has been enough for most of my stuff.

    Another option that is a bit different but surprisingly awesome is Visual Event:
    http://www.sprymedia.co.uk/article/Visual+Event+2

    It highlights all of the elements on a page that have been bound and has popovers showing the functions that are called. Pretty nifty for a bookmark! There's a Chrome plugin as well if that's more your thing - not sure about other browsers.

    AnonymousAndrew has also pointed out monitorEvents(window); here

    极致的悲 2024-10-02 06:05:37

    看起来 Firebug (Firefox 插件)有答案:

    • 打开 Firebug
    • 右键单击​​ HTML 选项卡中的元素
    • 单击 日志事件
    • 启用控制台选项卡
    • 单击在控制台选项卡中保留(否则重新加载页面后控制台选项卡将清除)
    • 选择关闭(手动)
    • 控制台选项卡中将出现类似的内容:

      <前><代码>...
      鼠标移动客户端X = 1097,客户端Y = 292
      弹出显示
      鼠标按下 clientX=1097, clientY=292
      重点
      mouseup clientX=1097, clientY=292
      单击客户端X = 1097,客户端Y = 292
      鼠标移动客户端X = 1096,客户端Y = 293
      ...

    来源:Firebug 提示:日志事件< /a>

    Looks like Firebug (Firefox add-on) has the answer:

    • open Firebug
    • right click the element in HTML tab
    • click Log Events
    • enable Console tab
    • click Persist in Console tab (otherwise Console tab will clear after the page is reloaded)
    • select Closed (manually)
    • there will be something like this in Console tab:

      ...
      mousemove clientX=1097, clientY=292
      popupshowing
      mousedown clientX=1097, clientY=292
      focus
      mouseup clientX=1097, clientY=292
      click clientX=1097, clientY=292
      mousemove clientX=1096, clientY=293
      ...
      

    Source: Firebug Tip: Log Events

    谜兔 2024-10-02 06:05:37

    对于 Chrome,请通过命令行 API 查看 monitorEvents()。

    • 通过菜单>打开控制台工具> JavaScript 控制台。

    • 输入monitorEvents(window);

    • 查看充斥着事件的控制台

      <前><代码> ...
      mousemove MouseEvent {dataTransfer: ...}
      mouseout MouseEvent {dataTransfer: ...}
      鼠标悬停 MouseEvent {dataTransfer: ...}
      更改事件 {clipboardData: ...}
      ...

    文档。我猜这个功能是在上一个答案之后添加的。

    Regarding Chrome, checkout the monitorEvents() via the command line API.

    • Open the console via Menu > Tools > JavaScript Console.

    • Enter monitorEvents(window);

    • View the console flooded with events

       ...
       mousemove MouseEvent {dataTransfer: ...}
       mouseout MouseEvent {dataTransfer: ...}
       mouseover MouseEvent {dataTransfer: ...}
       change Event {clipboardData: ...}
       ...
      

    There are other examples in the documentation. I'm guessing this feature was added after the previous answer.

    夜访吸血鬼 2024-10-02 06:05:37

    您可以在 Google 中使用 getEventListeners Chrome 开发者控制台

    getEventListeners(object) 返回注册的事件监听器
    指定的对象。

    getEventListeners(document.querySelector('option[value=Closed]'));
    

    You can use getEventListeners in your Google Chrome developer console.

    getEventListeners(object) returns the event listeners registered on
    the specified object.

    getEventListeners(document.querySelector('option[value=Closed]'));
    
    把时间冻结 2024-10-02 06:05:37

    @Himaas——Firebug 已被 Firefox 开发者版取代。如果您继续安装它,您可以通过打开开发工具(右键单击>检查)来记录事件,然后选择调试器,然后在页面底部您应该看到“事件侦听器断点”,其中有一个名为“Event Listener Breakpoints”的未选中复选框“日志”。选中该复选框。现在您所要做的就是在“事件侦听器断点”下显示的列表中选择要记录的事件。从那里您将看到记录到控制台的选定事件。

    这是一张有助于说明的图片:
    输入图片此处描述

    @Himaas -- Firebug has been replaced in favor of Firefox Developer Edition. If you go ahead and install it, you can log events by opening the dev tools (Right Click > Inspect), then select Debugger, and then toward the bottom of the page you should see "Event Listener Breakpoints" with an unchecked checkbox called "Log". Check that checkbox. Now all you have to do is select the events you want logged in the list presented under "Event Listener Breakpoints". From there you will see the selected events logged to the console.

    Here is an image to help illustrate:
    enter image description here

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文