简体   繁体   English

如何检查事件是否被触发?

[英]How to check if an event was fired or not?

I'm using Chrome DevTools to debug JavaScript. 我正在使用Chrome DevTools来调试JavaScript。 In my script I bound a click event to an element using the jQuery bind() method. 在我的脚本中,我使用jQuery bind()方法bind() click事件绑定到元素。 How to check if that event was fired or not? 如何检查该事件是否被触发?

Edit 编辑
Sorry because I wasn't so specific, I know that I can use console.log() or set a breakpoint inside the event listener body. 抱歉,因为我没那么具体,我知道我可以使用console.log()或在事件监听器体内设置断点。 What I'm talking about here is an out of box feature of the Chrome DevTools that allows you to check that without using the console, eg a tab that contains all the events that were fired with related information. 我在这里谈论的是Chrome DevTools的开箱即用功能,它允许您在不使用控制台的情况下进行检查,例如包含所有使用相关信息触发的事件的选项卡。

Regarding Chrome, checkout the monitorEvents() via the command line API. 关于Chrome,请通过命令行API检查monitorEvents()。

  • Open the console via Menu > Tools > JavaScript Console. 通过菜单>工具> JavaScript控制台打开控制台。
  • Enter monitorEvents(window); 输入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. 我猜这个功能是在上一个答案之后添加的。

Use console.log() for more user friendly check (console must be opened of course to see the result): 使用console.log()进行更加用户友好的检查(当然必须打开控制台才能看到结果):

$("#myElement").bind("click", function() {
   console.log("Fired!");
});

Or you can alert it, which is much more anoying tho: 或者你可以提醒它,这更令人讨厌:

$("#myElement").bind("click", function() {
   alert("Fired!");
});

You can try below code to check if event fire or not. 您可以尝试以下代码来检查事件是否触发。

var eventFire = false;

$("button").on("click", function() {
   eventFire = true;
if(eventFire){
    alert('Event Fired')
}   
});   

I normally use an alert (because we develop on Rails -- console is unreliable): 我通常使用警报(因为我们在Rails上开发 - 控制台不可靠):

$(document).on("event", "#element", function() { 
    alert("event");
});

The bottom line is you're going to have to trigger some "visible" thing to see if the event fired. 最重要的是,您将不得不触发一些“可见”的事情来查看事件是否被触发。 If it doesn't, you'll either not be binding to the correct element, or your event won't be firing 如果没有,您将不会绑定到正确的元素,或者您的事件将不会被触发

Hope this helps! 希望这可以帮助!

Using jQuery Try the following: 使用jQuery尝试以下方法:

$("#el").on("click", function() {
    console.log("Fired!");
});

You can then see if the event was fired or not. 然后,您可以查看事件是否被触发。 So basically Just add a log in your function of where the event is triggering should do the trick. 所以基本上只需在事件触发的函数中添加一个日志即可。

You can put an "alert('message');" 你可以发出“警告('消息');” in your event handler and see a popup each time the handler fires. 在你的事件处理程序中,每次处理程序触发时都会看到一个弹出窗口。 Or you can put a "console.log('message');" 或者你可以把“console.log('message');” in your event handler and review the log to see all the times that the event fired. 在您的事件处理程序中并查看日志以查看事件触发的所有时间。 Or you can put "debugger" in your event handler and step through your code as it executes in the chrome debugger. 或者,您可以将“调试器”放在事件处理程序中,并在Chrome调试器中执行时逐步执行代码。 There is also a way to dynamically find an event handler and insert a debugger break point in your code using the chrome dev tools elements panel. 还有一种方法可以使用chrome dev工具元素面板动态查找事件处理程序并在代码中插入调试器断点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM