All you need to know about line breaks

栏目: IT技术 · 发布时间: 4年前

内容简介:It’s aThere are two straightforward solutions to make ‘\n’ work in html

All you need to know about line breaks

What’s downwards arrow with corner leftwards (↵)

It’s a unicode code character with code point 8629 .

let s = '↵';
let codePont = s.charCodeAt(0); // 8629
let hex = codePoint.toString(16); // 21b5
let hexFormat = '\u21b5'; // '↵'
The character is a human-friendly representation of a newline in the console, like \n is in JavaScript
let arr = ['a\nb', 'a↵b'];
console.log(arr);
/*
["a↵b", “a↵b”]    // note they look the same on console
*/

console.log(arr[0]);
/*
"a
b"  // note it’s indeed a line break
*/

console.log(arr[1]);
/*
"c↵d" // note it’s indeed an arrow
*/

console.log(arr[0] === arr[1]); // false

How the developer tools express a new line in debugging output and how they treat the same character in source code are two different things

How to preserve line break in html

Whitespace in html is compressed into a single space by default
document.write('Hello\nWorld');
// You will see 'Hello World' on the page

There are two straightforward solutions to make ‘\n’ work in html

Convert \n to br tag

: The Line Break element

document.write('Hello\nWorld'.replace(/\n/g, '<br/>'))

pre tag

The HTML pre tag defines reformatted text.

css white-space

The white-space CSS proper sets how white space inside an element is handled. Possible values are:

  • normal: Sequences of whitespace will collapse into a single whitespace. Text will wrap when necessary.
  • nowrap: Sequences of whitespace will collapse into a single whitespace. Text will never wrap to the next line. The text continues on the same line until a
    tag is encountered.
  • pre: Whitespace is preserved by the browser. Text will only wrap on line breaks. Acts like the
    tag in HTML.
  • pre-wrap: Whitespace is preserved by the browser. Text will wrap when necessary, and on line breaks.

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

互联网的误读

互联网的误读

詹姆斯•柯兰(James Curran)、娜塔莉•芬顿(Natalie Fenton)、德 斯•弗里德曼(Des Freedman) / 何道宽 / 中国人民大学出版社 / 2014-7-1 / 45.00

互联网的发展蔚为壮观。如今,全球的互联网用户达到20亿之众,约占世界人口的30%。这无疑是一个新的现象,对于当代各国的经济、政治和社会生活意义重大。有关互联网的大量大众读物和学术著作鼓吹其潜力将从根本上被重新认识,这在20世纪90年代中期一片唱好时表现尤甚,那时许多论者都对互联网敬畏三分,惊叹有加。虽然敬畏和惊叹可能已成过去,然而它背后的技术中心主义——相信技术决定结果——却阴魂不散,与之伴生的则......一起来看看 《互联网的误读》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具