Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 换行字符串_Javascript_Jquery_Css - Fatal编程技术网

Javascript 换行字符串

Javascript 换行字符串,javascript,jquery,css,Javascript,Jquery,Css,我从github api模块返回了一个字符串,其格式如下: Added debug mode for developers Simply add the class DEBUG to the body and you'll notice little boxes scattered throughout your site attached to each grid item. Added debug mode for developers Simply add the class DEBU

我从github api模块返回了一个字符串,其格式如下:

Added debug mode for developers

Simply add the class DEBUG to the body and you'll notice little boxes
scattered throughout your site attached to each grid item.
Added debug mode for developers Simply add the class DEBUG to the body and you'll notice little boxes
scattered throughout your site attached to each grid item.
我想知道是否有一个css选项可以清除这些行,因为浏览器读到上面这样的内容:

Added debug mode for developers

Simply add the class DEBUG to the body and you'll notice little boxes
scattered throughout your site attached to each grid item.
Added debug mode for developers Simply add the class DEBUG to the body and you'll notice little boxes
scattered throughout your site attached to each grid item.
我真的不想用JavaCScript将字符串分开,但如果必须这样做,我想我会:)

我试过
断字:断字但是,这只是将行拆分为没有空格的较长字符串。
我还尝试了
空白:nowrap但是这实际上并没有包装

如果我用控制台记录数据,chrome有两个
导致换行的单词“developers”后面的字符,javascript是否可以选择这些字符


任何帮助都会很好

您的问题有点不清楚,但我假设您希望保留字符串中包含的换行符,而不是浏览器默认完全忽略它们

对于
空白
属性,您需要一个
pre
选项

/* Display text exactly as entered */
white-space: pre;

/* Display as entered, plus extra linebreaks as needed for long lines */
white-space: pre-wrap;

/* Display newlines as entered, plus extra linebreaks as needed for long lines */
white-space: pre-line;
关于差异的一些注释:


  • pre
    相当于将整个东西包装在一个
    中,我认为没有一种方法可以用css来实现。javascript方式:

    text = text.replace(/(\n\s*\n)|\n/g, "$1 ")
    

    这里没有html,它是一个完整的字符串,像上面那样用换行符格式化如果字符串是从API返回的,你不是已经把它作为JS字符串了吗?如果是这样,只需将换行符替换为

    标记。没有新行符可替换?如何使用换行符格式化字符串而不使用新行符?(或者它是否有回车符?肯定是一个还是另一个,或者两者都有?)也许你应该展示你用来从API中获取字符串的代码,并将其放在你的页面上。@ShannonHochkins“清除这些行”的意思有点不清楚。我的回答假设您的意思是要显示字符串中的换行符,而不是浏览器默认的一起运行所有内容。请编辑你的问题,让它更清楚你真正想要的是什么。虽然这让我接近了,但它还是有点奇怪:/你说的“有点奇怪”是什么意思?对不起,我应该更清楚,它在右手边,你会看到和above@ShannonHochkins使用
    pre-line
    pre-wrap
    而不是
    pre
    ,并从您不需要的地方删除换行符。@Shannon Hochkins您可以自动删除单换行符并保留双换行符,就像降价一样:
    替换(/(\n\s+\n)|\n/,“$1”)
    嗯。。。该循环可以很好地进行重构,以在循环头中使用条件,从而提高可读性。提示:
    do。。。虽然
    这不起作用,但如果有帮助的话,我已经将数据记录在控制台中,导致字符串中的行实际中断的原因是:
    。虽然我不知道那是什么?我只是举个例子。它起作用了。他可以随心所欲地重构它。“这正是我的想法。”ShannonHochkins说,这是一个新角色。它是一个不可见的字符,使字符串开始到一个新行,就像按回车键一样。那么这个字符可以通过javascript选择吗?