如何在 Google Chrome Javascript 控制台中编写和运行脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5524949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write and run scripts in the Google Chrome Javascript Console?
提问by Paolo
I recently switched from Firefox to Chrome and I (probably) missed an important feature. I was used to test javascript snippets on FF from within the Firebug console this way: open the console, write the script and finally press CTRL + Return to execute the code.
我最近从 Firefox 切换到 Chrome,我(可能)错过了一个重要功能。我曾经用这种方式在 Firebug 控制台中测试 FF 上的 javascript 片段:打开控制台,编写脚本,最后按 CTRL + Return 执行代码。
It seems not possible to do the same in Chrome console, as when I type some code there and press return to start a new line the code is executed immediatly.
在 Chrome 控制台中似乎不可能做同样的事情,因为当我在那里输入一些代码并按回车键开始一个新行时,代码会立即执行。
Is there a way to replicate the Firefox behavior on Chrome?
有没有办法在 Chrome 上复制 Firefox 的行为?
Thanks.
谢谢。
采纳答案by naveen
Install Firebug Lite for Google Chrome. It has got a console.
Don't look for a full fledged Firebug. You will be disappointed :)
为 Google Chrome安装Firebug Lite。它有一个控制台。
不要寻找成熟的 Firebug。你会失望的 :)
Oops,I didn't read properly at first. My bad!
哎呀,我一开始没有正确阅读。我的错!
In Firebug Lite, take Console. Then you will see a tiny red up-arrow at the right corner.
Click on it and you will get a multi-line console. Won't you?
在 Firebug Lite 中,使用 Console。然后你会在右上角看到一个小的红色向上箭头。
单击它,您将获得一个多行控制台。你不会吗?
回答by bpierre
It seems that there is no explicit “multiline mode”. But you can:
似乎没有明确的“多行模式”。但是你可以:
- Paste code (it will preserve multiline)
- Shift + Return to add a new line without executing the script
- 粘贴代码(它将保留多行)
- Shift + Return 添加新行而不执行脚本
Related bugs:
https://bugs.webkit.org/show_bug.cgi?id=30553
http://code.google.com/p/chromium/issues/detail?id=72739
相关错误:
https: //bugs.webkit.org/show_bug.cgi? id =
30553 http://code.google.com/p/chromium/issues/detail?id=72739
回答by xiola
You can also hit Shift + Enter to start a new line without running the code in Chrome's console: https://developers.google.com/chrome-developer-tools/docs/tips-and-tricks#multiline-commands
您也可以按 Shift + Enter 开始新行,而无需在 Chrome 的控制台中运行代码:https: //developers.google.com/chrome-developer-tools/docs/tips-and-tricks#multiline-commands
回答by shruti
Better way of doing this using Chrome featue i.e Snippetswhere you can writejavascriptand saveit in chrome developer console. Its available underneath source inside developers tools while inspecting element. More info about the snippets can be find on this link.
使用 Chrome featue 执行此操作的更好方法 IE Snippets,您可以在其中编写javascript并将其保存在 chrome 开发人员控制台中。在检查元素时,它可以在开发人员工具中的源代码下使用。 可以在此链接上找到有关片段的更多信息。
It was available in Chrome canary and I guess now it is available in default chrome browser also.
它在 Chrome canary 中可用,我想现在它也可以在默认的 chrome 浏览器中使用。
回答by Saeed Neamati
I recommend this:
我推荐这个:
- Write
debugger;
and hit Enter, in the console tab - This takes you to the Sourcestab; if not, make sure debugger breakpoints are active
- Now you can write whatever you want in the Sourcestab, which acts like a full IDE with features like newline and indentation
- Select any part of your code to run, and right-click, choose
Evaluate in console
- 在控制台选项卡中写入
debugger;
并按Enter 键 - 这会将您带到“来源”选项卡;如果没有,请确保调试器断点处于活动状态
- 现在您可以在Sources选项卡中编写任何您想要的内容,它就像一个完整的 IDE,具有换行和缩进等功能
- 选择要运行的代码的任何部分,然后右键单击,选择
Evaluate in console