简体   繁体   English

在javascript数组元素之间添加换行符

[英]adding line breaks between javascript array elements

I wonder if someone can help with this task; 我想知道是否有人可以帮助完成这项任务; I found this great code for a daily quote and I am just trying to put a line break between the daily quote and the author of the quote. 我找到了每日报价的出色代码,而我只是想在每日报价和报价的作者之间插入一个换行符。 I thought maybe creating a two dimensional array would make it easier, but unfortunately I couldn't figure out how to put the line break. 我以为创建二维数组可能会更容易,但是不幸的是我无法弄清楚如何放置换行符。 Thank you. 谢谢。 Here is the code so far: 这是到目前为止的代码:

<script type="text/JavaScript"> 

var quote = new Array()
for (i=0; i<7; i++){
quote[i] = new Array(6)
}

quote[0][0] = 'If you want to go fast go alone, if you want to go far go together!'; 
quote[0][1] = 'African Proverb';
quote[1][0] = 'We either make ourselves happy or miserable. The amount of work is the same.'; 
quote[1][1] = 'Carlos Castaneda'; 
quote[2][0] = 'Everything should be made as simple as possible, but not any simpler.'; 
quote[2][1] = 'Albert Einstein';
quote[3][0] = 'Failure is the mother of success.'; 
quote[3][1] = 'Chinese Proverb'; 
quote[4][0] = 'The first to apologize is the bravest. The first to forgive is the strongest. The first to forget is the happiest.' 
quote[4][1] = 'Unknown';
quote[5][0] = 'If an egg is broken by an outside force, life ends. If it is broken by an inside force life begins. Great things happen from the inside.'; 
quote[5][1] = 'Unknown';
quote[6][0] = 'Simplicity is the ultimate sophistication.' 
quote[6][1] = 'Leonardo Da Vinci';

var qlen = quote.length;
var firstDate = new Date(2016,2,25);//start date (yyyy,m,d) - m=0=January, m=1=February
var today = new Date();//today
var dif = Math.floor((today-firstDate)/1000/60/60/24);//difference in days
while(dif>=qlen){//calculate the index of the quote of the day 
dif=dif-qlen;//restart the array index if the difference is greater that the    array's length 
} 
var todayQuote = quote[dif][0];//the quote of the day 
var todayQuoteOwner = quote[dif][1];//author of the quote of the day

onload = function(){document.getElementById('q').firstChild.data = todayQuote + </n>  + document.getElementById('qu').firstChild.data = todayQuoteOwner}


</script> 

<div>  id='q'; </div>  

First thing I recommend is moving the <script></script> content to the end rather than the beginning, so that way there's no need for an onload function. 我建议的第一件事是将<script></script>内容移到末尾而不是开始,这样就不需要onload函数。

Your javascript also refers to a qu element but your html code didn't include it. 您的javascript还引用了qu元素,但您的html代码未包含它。 For the time being I'm going to assume it's just another div but feel free to correct me if I'm wrong. 目前,我将假定它只是另一个div但是如果我错了,请随时纠正我。

Your javascript also seems to set the quote and author to a firstChild.data but your html doesn't have any children in the div . 您的javascript也似乎将引号和作者设置为firstChild.data但是html中的div中没有任何子代。 So if you actually were to run this, there will be a null error therefore I've added a nested <p /> to each. 因此,如果您实际上要运行此命令,则将出现null错误,因此我为每个脚本添加了一个嵌套的<p /> And for the sake of testing, I've included innerHTML as well so you can see the results. 为了测试,我也包含了innerHTML ,以便您可以看到结果。

<div id="q"><p></p></div>
<div id="qu"><p></p></div>

<script type="text/JavaScript">
    // ... generate the quotes and authors ...
    var qElement = document.getElementById('q');
    var quElement = document.getElementById('qu');

    qElement.firstChild.innerHTML = qElement.firstChild.data = todayQuote;
    quElement.firstChild.innerHTML = quElement.firstChild.data = todayQuoteOwner;
</script>

If you're looking to just have one element container for both the quote and author, then just get rid of the qu element altogether and separate the two parts by a < br/> element. 如果您只想为引号和作者提供一个元素容器,则只需完全删除qu元素,并用< br/> qu元素将这两部分分开。

<div id="q"><p></p></div>

<script type="text/JavaScript">
    // ... generate the quotes and authors ...
    var qElement = document.getElementById('q');

    qElement.firstChild.innerHTML = qElement.firstChild.data = todayQuote + '<br/>' + todayQuoteOwner;
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM