jQuery 在控制台中跟踪变量时,如何创建新行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16239474/
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
When tracing out variables in the console, How to create a new line?
提问by Leon Gaban
So I'm trying to do something simple, I want to break up my traces in the console into several lines, using 1 console.log statement:
所以我正在尝试做一些简单的事情,我想使用 1 个 console.log 语句将控制台中的跟踪分成几行:
console.log('roleName = '+roleName+' role_ID = '+role_ID+' modal_ID = '+modal_ID+\n+'related = '+related);
How would you write the above to trace out the following?
你会如何写上面的内容来追踪下面的内容?
roleName = test
role_ID = test
modal_UD = test
related = test
instead of roleName = test role_ID = test modal_UD = test related = test
代替 roleName = test role_ID = test modal_UD = test related = test
I've checked out several other questions which appear similar, but none have helped or are talking about a different thing.
我检查了其他几个看起来相似的问题,但没有一个有帮助或正在谈论不同的事情。
Thanks for taking a look!
谢谢参观!
回答by Selvakumar Arumugam
You should include it inside quotes '\n'
, See below,
你应该把它包括在引号内 '\n'
,见下文,
console.log('roleName = '+roleName+ '\n' +
'role_ID = '+role_ID+ '\n' +
'modal_ID = '+modal_ID+ '\n' +
'related = '+related);
回答by Vlad Bezden
In ES6/ES2015 you can use string literal syntax called template literals. Template strings use backtick character instead of single quote ' or double quote marks ". They also preserve new line and tab
在 ES6/ES2015 中,您可以使用称为模板文字的字符串文字语法。模板字符串使用反引号字符而不是单引号 ' 或双引号 "。它们还保留了新行和制表符
const roleName = 'test1';
const role_ID = 'test2';
const modal_ID = 'test3';
const related = 'test4';
console.log(`
roleName = ${roleName}
role_ID = ${role_ID}
modal_ID = ${modal_ID}
related = ${related}
`);
回答by Andrei Nemes
Easy, \n
needs to be in the string.
很简单,\n
需要在字符串中。
回答by Kevin B
Why not just use separate console.log()
for each var, and separate with a comma rather than converting them all to strings? That would give you separate lines, AND give you the true value of each variable rather than the string representation of each (assuming they may not all be strings).
为什么不对console.log()
每个变量使用单独的,并用逗号分隔而不是将它们全部转换为字符串?这将为您提供单独的行,并为您提供每个变量的真实值,而不是每个变量的字符串表示形式(假设它们可能不都是字符串)。
console.log('roleName',roleName);
console.log('role_ID',role_ID);
console.log('modal_ID',modal_ID);
console.log('related',related);
And I think it would be easier to read/maintain.
而且我认为阅读/维护会更容易。
回答by Viktor Soroka
The worst thing of using just
最糟糕的事情只是使用
console.log({'some stuff': 2} + '\n' + 'something')
is that all stuff are converted to the string and if you need object to show you may see next:
是所有内容都转换为字符串,如果您需要显示对象,您可能会看到下一个:
[object Object]
Thus my variant is the next code:
因此我的变体是下一个代码:
console.log({'some stuff': 2},'\n' + 'something');
回答by misterzik
console.log('Hello, \n' +
'Text under your Header\n' +
'-------------------------\n' +
'More Text\n' +
'Moree Text\n' +
'Moooooer Text\n' );
This works great for me for text only, and easy on the eye.
这对我来说非常适合仅用于文本,并且易于使用。
回答by Justin Bicknell
You need to add the new line character \n
:
您需要添加新行字符\n
:
console.log('line one \nline two')
would display:
会显示:
line one
第一行
line two
第二行