我想通过代码强制Chrome调试器在一行上中断,或者使用某种注释标记,例如console.break()。


当前回答

设置按钮点击监听器并调用调试器;

例子

$("#myBtn").click(function() {
 debugger;   
});

Demo

http://jsfiddle.net/hBCH5/

JavaScript中调试的资源

http://www.laurencegellert.com/2012/05/the-three-ways-of-setting-breakpoints-in-javascript/ http://berzniz.com/post/78260747646/5-javascript-debugging-tips-youll-start-using-today

其他回答

你也可以设置debug(functionName)来调试函数。

https://developers.google.com/web/tools/chrome-devtools/javascript/breakpoints#function

设置按钮点击监听器并调用调试器;

例子

$("#myBtn").click(function() {
 debugger;   
});

Demo

http://jsfiddle.net/hBCH5/

JavaScript中调试的资源

http://www.laurencegellert.com/2012/05/the-three-ways-of-setting-breakpoints-in-javascript/ http://berzniz.com/post/78260747646/5-javascript-debugging-tips-youll-start-using-today

我不推荐调试器;如果你只是想杀死和停止javascript代码,因为调试器;将只是暂时冻结你的javascript代码,而不是永久停止它。

如果你想在你的命令下正确地杀死和停止javascript代码,请使用以下命令:

抛出新的错误(“这个错误消息出现是因为我放置了它”);

调试器是EcmaScript保留的关键字,从ES5开始提供可选语义

因此,它不仅可以在Chrome中使用,还可以通过节点调试myscript.js在Firefox和node .js中使用。

标准说:

Syntax DebuggerStatement : debugger ; Semantics Evaluating the DebuggerStatement production may allow an implementation to cause a breakpoint when run under a debugger. If a debugger is not present or active this statement has no observable effect. The production DebuggerStatement : debugger ; is evaluated as follows: If an implementation defined debugging facility is available and enabled, then Perform an implementation defined debugging action. Let result be an implementation defined Completion value. Else Let result be (normal, empty, empty). Return result.

ES6没有变化。

正如其他人已经说过的,调试器;才是正确的选择。 我写了一个小脚本,你可以在浏览器的命令行中使用它来设置和删除函数调用之前的断点: http://andrijac.github.io/blog/2014/01/31/javascript-breakpoint/