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

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

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


当前回答

如果你只是想在调试时清除控制台,你可以简单地点击“ban-circle”⃠按钮来清除console.log。

或者,只需按“Ctrl+L”来清除控制台使用你的键盘。

其他回答

铬:

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()。

当在Chrome中本地调试时,我使用以下方法来别名cls(将以下JavaScript输入控制台):

Object.defineProperty(window, 'cls', {
    get: function () {
        return console.clear();
    }
});

现在在控制台中输入CLS将清除控制台。

在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();

根据Cobbal的回答,以下是我的做法:

在我的JavaScript,我把以下:

setInterval(function() {
  if(window.clear) {
    window.clear();
    console.log("this is highly repeated code");
  }
}, 10);

条件代码在你ASSIGN窗口之前不会运行。清除(意思是您的日志是空的,直到您这样做)。在调试控制台类型中:

window.clear = clear;

Violà -一个自我清除的日志。

Mac OS 10.6.8 - Chrome 15.0.874.106