Javascript 如何在数组中的字符串中开始新行?
我在CodePen上创建了一个随机引用生成器(url:) 我在JS中的数组中设置了我的引号,但我希望引号的作者位于新行 我已尝试在需要换行符的位置插入\n:Javascript 如何在数组中的字符串中开始新行?,javascript,arrays,Javascript,Arrays,我在CodePen上创建了一个随机引用生成器(url:) 我在JS中的数组中设置了我的引号,但我希望引号的作者位于新行 我已尝试在需要换行符的位置插入\n: var quotes = [ "How many cares one loses when one decides not to be something but to be someone. \n -Gabrielle “Coco” Chanel", "Be who you are and say what you feel, becau
var quotes = [
"How many cares one loses when one decides not to be something but to be someone. \n -Gabrielle “Coco” Chanel",
"Be who you are and say what you feel, because those who mind don’t matter and those who matter don’t mind. \n -Dr. Seuss",
"Imitation is suicide. \n -Ralph Waldo Emerson", ...]
您正在通过
innerHTML
将该字符串作为HTML注入,因此需要将\n
替换为用于在HTML中开始新行的标记,即:br
document.querySelector('#test').innerHTML='BLA BLA
BLA'代码>
如果您的引号
数组将是静态的,请尝试从字符串中拆分出\n
,并将其替换为
。否则,您可以手动将引号数组中的\n
替换为
我制作了一个自定义函数checkseparator
,它通过使用\n
拆分字符串
,并与
联接,返回最终确定的字符串
function checkSeperator(str) {
if (str.includes("\n")) {
var finalStr = str.split("\n").join(" <br/> ");
return finalStr;
} else {
return str;
}
}
功能检查分隔符(str){
如果(str.includes(“\n”)){
var finalStr=str.split(“\n”).join(
);
返回finalStr;
}否则{
返回str;
}
}
最终代码:
var quotes = [
"How many cares one loses when one decides not to be something but to be someone. \n -Gabrielle “Coco” Chanel",
"Be who you are and say what you feel, because those who mind don’t matter and those who matter don’t mind. <br/> -Dr. Seuss",
"We must not allow other people’s limited perceptions to define us. \n -Virginia Satir",
"Don’t look for society to give you permission to be yourself. \n -Steve Maraboli",
"If things go wrong, don’t go with them. \n -Roger Babson",
"Wanting to be someone else is a waste of who you are. \n -Kurt Cobain",
"Tension is who you think you should be. Relaxation is who you are. \n -Chinese Proverb",
"Where’s your will to be weird? \n -Jim Morrison",
"Some people say you are going the wrong way, when it’s simply a way of your own. \n -Angelina Jolie",
"Remember to always be yourself. Unless you suck. \n -Joss Whedon",
"Do what you can, with what you have, where you are. \n -Theodore Roosevelt",
"To find yourself, think for yourself. \n -Socrates",
"If you seek authenticity for authenticity’s sake you are no longer authentic. \n -Jean Paul Sartre",
"Do what you must, And your friends will adjust. \n -Robert Brault",
"Just let awareness have its way with you completely.<br/> -Scott Morrison",
"We must be our own before we can be another’s. <br/> -Ralph Waldo Emerson",
"This above all: to thine own self be true. <br/> -William Shakespeare"
];
function newQuote() {
var randomNumber = Math.floor(Math.random() * quotes.length);
document.getElementById("quoteDisplay").innerHTML = checkSeperator(
quotes[randomNumber]
);
}
function checkSeperator(str) {
if (str.includes("\n")) {
var finalStr = str.split("\n").join(" <br/> ");
return finalStr;
} else {
return str;
}
}
var引号=[
“当一个人决定不做某件事而做某个人时,他会失去多少关心。\n-加布里埃尔“可可”香奈儿“,
“做你自己,说出你的感受,因为在乎的人无所谓,在乎的人无所谓。
-苏斯博士”,
“我们不能让别人有限的感知来定义我们。\n-弗吉尼亚·萨蒂尔”,
“不要指望社会允许你做你自己。\n-史蒂夫·马拉博利”,
“如果事情出了问题,不要跟着他们走。\n-罗杰·巴布森”,
“想成为别人是在浪费你自己。\n-库尔特·科班”,
“紧张是你认为你应该成为的人。放松是你自己。\n-中国谚语”,
“你想变得古怪的意愿在哪里?\n-吉姆·莫里森”,
“有些人说你走错了路,而这只是你自己的路。\n-安吉丽娜·朱莉”,
“记住永远做你自己。除非你很差劲。\n-Joss Whedon”,
“尽你所能,用你所拥有的,你在哪里。\n-西奥多·罗斯福”,
“要找到自己,就要自己思考。\n-苏格拉底”,
“如果你为了真实而追求真实,你就不再是真实的。\n-让-保罗·萨特”,
“做你必须做的事,你的朋友会适应的。\n-罗伯特·布拉特”,
“让意识完全随你的便。
-斯科特·莫里森”,
“在成为别人的人之前,我们必须成为自己的人。
-拉尔夫·沃尔多·爱默生,
“最重要的是:忠于自己。
-威廉·莎士比亚”
];
函数newQuote(){
var randomNumber=Math.floor(Math.random()*quotes.length);
document.getElementById(“quoteDisplay”).innerHTML=checkSeparator(
引号[随机数字]
);
}
功能检查分离器(str){
如果(str.includes(“\n”)){
var finalStr=str.split(“\n”).join(
);
返回finalStr;
}否则{
返回str;
}
}
您可以将此代码粘贴到js
选项卡中的codepen中,它将正常工作。谢谢,这为我澄清了问题并节省了一点时间!我很乐意提供帮助