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

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

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


当前回答

总有一个好把戏:

console.log("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

或者是上面的一个更短的变体:

console.log('\n'.repeat('25'));

这不是最优雅的解决方案,我知道:)…但作品。

对我来说,我通常只打印一个很长的“-----”分隔行,以帮助更容易阅读日志。

其他回答

console._inspectorCommandLineAPI.clear()

这是可行的

不用输入command,只需按:

CLTRL + L

清除chrome控制台

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

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

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

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

铬:

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