2016-03-27 27 views
0

誰かがこの作業を手伝うことができるのだろうかと思います。私は毎日の見積もりの​​ためのこの素晴らしいコードを見つけました、そして、私はちょうど日々の見積もりと見積もりの​​著者の間に改行を入れようとしています。多分2次元配列を作成する方が簡単だと思っていたのですが、残念ながら改行の仕方を理解できませんでした。ありがとうございました。ここでのコードは、これまでのところです:私はお勧めjavascript配列要素の間に改行を追加する

<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> 
+0

<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> 
'のdocument.getElementById( 'Q')。firstChild.data = todayQuote + "\ R \ n" +のdocument.getElementById( 'QU')。firstChild.data = todayQuoteOwner'? – Gogol

+0

またはhtmlに出力する場合、 'document.getElementById( 'q')。firstChild.data = todayQuote +"
"+ document.getElementById( 'qu')。firstChild.data = todayQuoteOwner'? – Gogol

+0

はありません。 –

答えて

0

まず最初は終わりではなく始まりに<script></script>コンテンツを移動しているので、オンロード機能のための必要はありませんそのように。

あなたのjavascriptもqu要素を参照していますが、htmlコードには含まれていません。当分の間、私はそれがちょうど別のものだと仮定しようとしていますが、私が間違っていれば、私を修正してください。

あなたのjavascriptは引用と著者をfirstChild.dataに設定しているようですが、htmlにはdivの子はありません。したがって、実際にこれを実行する場合、nullエラーが発生するため、それぞれに<p />をネストしました。テストのために、私はinnerHTMLも含めて結果を見ることができます。あなただけの引用と著者の両方のための1つの要素の容器を持っているために探している場合は、単に完全qu要素を取り除くと< br/>要素によって二つの部分を分離

<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> 

+0

ありがとう、魅力的に働いた。有益でよく説明されています。 – Fusion53

関連する問題