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

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

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


当前回答

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

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
}

其他回答

在Mac上,你也可以像在终端中一样使用⌘+K。

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

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

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

console._inspectorCommandLineAPI.clear()

这是可行的

这似乎很有效:

console.clear();

铬:

console._commandLineAPI.clear();

Safari:

console._inspectorCommandLineAPI.clear();

你可以创建自己的变量,它可以在以下两种情况下工作:

if (typeof console._commandLineAPI !== 'undefined') {
    console.API = console._commandLineAPI;
} else if (typeof console._inspectorCommandLineAPI !== 'undefined') {
    console.API = console._inspectorCommandLineAPI;
} else if (typeof console.clear !== 'undefined') {
    console.API = console;
}

在此之后,您可以简单地使用console.API.clear()。