我想知道我是否可以清除控制台的一些命令。

Console.log(),可以打印…是否有清除控制台的命令?

我已经尝试console.log(控制台);得到下面这个函数。

assert: function assert() { [native code] }
constructor: function Console() { [native code] }
count: function count() { [native code] }
debug: function debug() { [native code] }
dir: function dir() { [native code] }
dirxml: function dirxml() { [native code] }
error: function error() { [native code] }
group: function group() { [native code] }
groupEnd: function groupEnd() { [native code] }
info: function info() { [native code] }
log: function log() { [native code] }
markTimeline: function markTimeline() { [native code] }
profile: function profile() { [native code] }
profileEnd: function profileEnd() { [native code] }
time: function time() { [native code] }
timeEnd: function timeEnd() { [native code] }
trace: function trace() { [native code] }
warn: function warn() { [native code] }
__proto__: Object

我想没有办法清理控制台……但我希望有人对我说……]


当前回答

在Chrome控制台用鼠标右键点击,我们有清除控制台的选项

其他回答

一个方便的编译,用于以编程方式清除控制台的多个答案(从脚本,而不是控制台本身):

if(console._commandLineAPI && console._commandLineAPI.clear){
    console._commandLineAPI.clear();//clear in some safari versions
}else if(console._inspectorCommandLineAPI && console._inspectorCommandLineAPI.clear){
    console._inspectorCommandLineAPI.clear();//clear in some chrome versions
}else if(console.clear){
    console.clear();//clear in other chrome versions (maybe more browsers?)
}else{
    console.log(Array(100).join("\n"));//print 100 newlines if nothing else works
}

在Chrome控制台用鼠标右键点击,我们有清除控制台的选项

在MacOS:

Chrome - CMD+K Safari - CMD+K Firefox -没有快捷方式

在Linux上:

Chrome - CTRL+L Firefox -没有快捷方式

在Windows上:

Chrome - CTRL+L Ie - ctrl + l 边- CTRL+L Firefox -没有快捷方式

为了让它在Firefox中工作,可以使用用户脚本。下载FF的GreaseMonkey扩展。

document.addEventListener("keydown",function(event){
    if(event.metaKey && event.which==75) //CMD+K
    {
        console.clear();
    }
});

在脚本中,用值//@include *://*/*更新元数据,使其在每个页面上运行。只有当焦点在页面上时,它才会工作。这只是个变通办法。

如果你使用console.clear(),这似乎在chrome中工作。注意,它将输出“控制台已清除”消息。

注意,我在清除控制台后得到了一个错误,所以它不会禁用控制台,只清除它。此外,我只在chrome中尝试过,所以我不知道它是如何跨浏览器的。

编辑:我在Chrome、IE、Firefox和Opera中进行了测试。它适用于Chrome, MSIE和Opera的默认控制台,但不适用于Firefox,然而,它可以在Firebug中工作。

我要补充一点,因为这是谷歌上的搜索结果。

当使用ExtJS / Javascript时,我插入这个,控制台被清除-除非有错误。

console.log('\033[2J');

我很可能偏离了轨道,但这就是我在每次页面加载/刷新时清除控制台的方式。