2016-04-16 13 views
1

私は配列とDOMを使用して、配列に格納されている引用符を画面に表示しようとしています。私は出力を得ているので、私は現在、1つのインスタンスでドキュメントオブジェクトモデルを使用していると信じています。しかし、他の例では、画面上に何も表示できません(日付とその日の見積もりは画面に表示されません)。私は見て、私が間違ってやっていることを見つけようと試みましたが、成功しませんでした。これは割り当てのためのもので、DOMを使用する必要があります。だから、ここで私は誰かが正しい方向に私を向けることを望んでいる。どんな提案も大歓迎です。ありがとうございました!DOMを使用して何も表示されない

<html> 
<head> 
<meta charset="utf-8"> 
<title>JSJQ Assignment 4 - Arrays/Date Starter File</title> 

<style> 
    body{ 
     font-family: arial, sans-serif; 
     font-size: 100%; 
    } 
    form{ 
     margin-top: 50px; 
    } 
</style> 

<script> 

    //************************************************************ 
    // 1: define variables for today's date, 
    //************************************************************ 
    var today = new Date(); 
    var day = today.getDate(); 
    var month = today.getMonth(); 
    var year = today.getFullYear(); 

    //************************************************************ 
    // 2: define an array to hold the day names 
    //************************************************************ 
    var monthArray = new Array(); 

    monthArray[0] = "January"; 
    monthArray[1] = "February"; 
    monthArray[2] = "March"; 
    monthArray[3] = "April"; 
    monthArray[4] = "May"; 
    monthArray[5] = "June"; 
    monthArray[6] = "July"; 
    monthArray[7] = "August"; 
    monthArray[8] = "September"; 
    monthArray[9] = "October"; 
    monthArray[10] = "November"; 
    monthArray[11] = "December"; 

    var dayArray = new Array(); 

    dayArray[0] = "Monday"; 
    dayArray[1] = "Tuesday"; 
    dayArray[2] = "Wednesday"; 
    dayArray[3] = "Thursday"; 
    dayArray[4] = "Friday"; 
    dayArray[5] = "Saturday"; 
    dayArray[6] = "Sunday"; 


    //************************************************************ 
    // 3: define an array to hold the daily quotes 
    //************************************************************ 

    var quoteArray = new Array(); 

    quoteArray[0] = "Ability is nothing without opportunity - Napoleon Bonaparte"; 
    quoteArray[1] = "Nothing happens unless first we dream - Carl Sandburg"; 
    quoteArray[2] = "Believe you can and you're halfway there - Theodore Roosevelt"; 
    quoteArray[3] = "A place for everything, everything in its place - Benjamin Franklin"; 
    quoteArray[4] = "Don't let the fear of striking out hold you back - Babe Ruth"; 
    quoteArray[5] = "We can't help everyone, but everyone can help someone - Ronald Reagan"; 
    quoteArray[6] = "With self-discipline most anything is possible - Theodore Roosevelt"; 


    //************************************************************ 
    // 4: loop through all of the quotes 
    // and write the quotes to the page. Use DOM methods or innerHTML 
    // to write to the page. 
    //************************************************************ 
    function allQuotes() { 

     var allQuotes = document.getElementById('quotes'); 

     for (var i = 0; i < quoteArray.length; i++) { 

      var text = document.createTextNode(quoteArray[i]); 
      var br = document.createElement('br'); 

      allQuotes.appendChild(text); 
      allQuotes.appendChild(br); 
     } 

     quoteOfTheDay(); 
    } 

    //************************************************************ 
    // 5: create a window.onload function to format and display 
    // the current day name. 
    // 
    // Display the quote for the day. 
    // 
    //  
    //************************************************************ 

    function quoteOfTheDay() { 
     document.getElementById('quote_of_the_day').firstChild.nodeValue = quoteArray[today.getDay()-1]; 

     document.getElementById('date').firstChild.nodeValue = dayArray[day] + ", " + monthArray[month] + day + ", " + year; 
    } 

    window.onload = allQuotes; 

</script> 

</head> 

<body> 
<h1>Quote of the Day</h1> 
<p id="quote_of_the_day"></p> 
<p id="date"></p> 

<h2>All the quotes:</h2> 
<p id="quotes"></p> 
</body> 
</html> 
+0

あなたのコードをコピーして保存し、ChromeでHTMLファイルを開いたところです。私はすべての引用符を見ることができる、唯一欠けているのは今日の引用 – Satya

+0

@サティヤです。私は開始の 'ボディ'タグを見つけることができません。 – Sparky256

+0

@MatthewSpir​​eはあなたのスクリプトを置いた後、セクションの本体が終了します。私が何が起こったのか教えてください。 –

答えて

0

スクリプトコードにいくつかの論理的な間違いがあります。

HTML

<html> 
<head> 
</head> 
<title>JSJQ Assignment 4 - Arrays/Date Starter File</title> 
<body> 
<h1>Quote of the Day</h1> 
<p id="quote_of_the_day"></p> 
<p id="date"></p> 
<h2>All the quotes:</h2> 
<p id="quotes"></p> 
</body> 
</html> 

スクリプト

var today = new Date(); 
var day = today.getDate(); 
var month = today.getMonth(); 
var year = today.getFullYear(); 

//************************************************************ 
// 2: define an array to hold the day names 
//************************************************************ 
var monthArray = new Array(); 

monthArray[0] = "January"; 
monthArray[1] = "February"; 
monthArray[2] = "March"; 
monthArray[3] = "April"; 
monthArray[4] = "May"; 
monthArray[5] = "June"; 
monthArray[6] = "July"; 
monthArray[7] = "August"; 
monthArray[8] = "September"; 
monthArray[9] = "October"; 
monthArray[10] = "November"; 
monthArray[11] = "December"; 

var dayArray = new Array(); 

dayArray[0] = "Monday"; 
dayArray[1] = "Tuesday"; 
dayArray[2] = "Wednesday"; 
dayArray[3] = "Thursday"; 
dayArray[4] = "Friday"; 
dayArray[5] = "Saturday"; 
dayArray[6] = "Sunday"; 


//************************************************************ 
// 3: define an array to hold the daily quotes 
//************************************************************ 

var quoteArray = new Array(); 

quoteArray[0] = "Ability is nothing without opportunity - Napoleon Bonaparte"; 
quoteArray[1] = "Nothing happens unless first we dream - Carl Sandburg"; 
quoteArray[2] = "Believe you can and you're halfway there - Theodore Roosevelt"; 
quoteArray[3] = "A place for everything, everything in its place - Benjamin Franklin"; 
quoteArray[4] = "Don't let the fear of striking out hold you back - Babe Ruth"; 
quoteArray[5] = "We can't help everyone, but everyone can help someone - Ronald Reagan"; 
quoteArray[6] = "With self-discipline most anything is possible - Theodore Roosevelt"; 


//************************************************************ 
// 4: loop through all of the quotes 
// and write the quotes to the page. Use DOM methods or innerHTML 
// to write to the page. 
//************************************************************ 
function allQuotes() { 

    var allQuotes = document.getElementById('quotes'); 

    for (var i = 0; i < quoteArray.length; i++) { 

     var text = document.createTextNode(quoteArray[i]); 

     var br = document.createElement('br'); 
    allQuotes.appendChild(text); 
     allQuotes.appendChild(br); 

    } 
    quoteOfTheDay(); 
} 

//************************************************************ 
// 5: create a window.onload function to format and display 
// the current day name. 
// 
// Display the quote for the day. 
// 
//  
//************************************************************ 

function quoteOfTheDay() { 

    document.getElementById('quote_of_the_day').firstChild.nodeValue = quoteArray[today.getDay()-1]; 

    document.getElementById('date').firstChild.nodeValue = dayArray[day] + ", " + monthArray[month] + day + ", " + year; } 


window.onload = allQuotes; 
+0

私はページへの書き込みにinnerHTMLプロパティを使用することはできません、I – MatthewSpire

+0

これは私のブラウザでうまく動作しようとしています –

+0

私のブラウザはまだヘッダーテキスト以外に何も表示していません。 – MatthewSpire

0

私は出発体タグを見つけることができないと、あなたが意図的にそれらを逃した場合を除き、あなたのコードは、で終わる必要があります。

</body> 
</html> 

私が最初にこの問題を修正し、それが助けかどうかを確認します。

+0

あなたはこれを説明できますか?あなたのブラウザや他の場所でこれを試しましたか? –

+0

@AkhileshSingh。これらの終了タグがない場合、コードは偶然にも機能します。開始タグが表示されますが、コード内にこれらの終了タグが表示されませんでした。 – Sparky256

+0

タグを使用すると、タグも使用できません。 –

1

あなたの問題は、ここであなただけwindow.onloadに一つの値を割り当てることができますので、ウィンドウのロードが終了したときにのみallQuotesが実行されます

window.onload = quoteOfTheDay; 
window.onload = allQuotes; 

です。あなたは、両方のウィンドウ負荷に呼ばれるようにしたい場合は機能でそれらをラップし、また、あなたがquote_of_the_daydateの最初の子を取得しようが、彼らは子を持たない

window.onload = function(){ 
    quoteOfTheDay(); 
    allQuotes(); 
}; 

をwindow.onloadすることを割り当てます。スペースや何かのようにコンテンツを配置したり、子ノードの値ではなく内容全体を設定したりすることができます。

+0

'onload' =関数は連続していても動作しますが、bodyタグ内に 'onload'関数を複数使用することはできません。用語 'onload'は一度しか使用できません。 – Sparky256

+0

私は訂正をしましたが、テストすると何も表示されません。何か案は? – MatthewSpire

+0

あなたのブラウザで見られるべきものがありません。具体的にしてください。これまでのところ、私たちは単に」バグを修正してきた – Sparky256

0

function quoteOfTheDay() { 
        document.getElementById('quote_of_the_day').firstChild.nodeValue = quoteArray[today.getDay() - 1]; 
     // document.getElementById('date').firstChild.nodeValue = dayArray[day] + ", " + monthArray[month] + day + ", " + year; 
    } 

であなたのquoteOfTheDay機能を置き換え、このメソッドを使用して、内のダミー素子を追加例えば、。コードはquote_of_the_day子要素を探しているが、それは空であるので、コードが失敗しているよう

<p id="quote_of_the_day"><span></span></p> 

、。

関連する問題