javascript
  • arrays
  • quote
  • 2016-11-14 57 views -1 likes 
    -1

    我對如何將作者(在下一行)添加到引號中有點撓頭。將作者分配給當天的報價

    我有一個數組,並添加了關於這個問題的意見。

    <script type = "text/JavaScript">  
    var quote = new Array();  
    quote[0] = ' quote 1 Lorem ipsum blah blah ' ;  
    quote[1] = ' quote 2 Nullam commodo blah blah ' ;  
    quote[2] = ' quote 3 Sed vitae blah blah ' ;  
    quote[3] = ' quote 4 Maecenas blah blah ' ;  
    quote[4] = ' quote 5 Fusce lorem velit, blah blah ' ;  
    quote[5] = ' quote 6 Ut blah blah ' ;  
    quote[6] = ' quote 7 Phasellus blah blah ' ;  
    // authors: Name[x] = ' name '; ? 
    var quotelen = quote.length;  
    
    // Pick a random index number 
    // Is this how to pick a random number from the array? 
    // var quotelen = Math.floor(Math.random() * quote.length); 
    
    
    // The Start Date (yyyy, m, d) m=0=January, m=1=February 
    var firstDate = new Date(2016,0,1);   
    
    // Today 
    var today = new Date();  
    
    // Difference in days 
    var diff = Math.floor((today - firstDate)/1000/60/60/24);  
    
    // Calculate the index of the quote of the day 
    while(diff >= quotelen){  
      
    // Restart the array index if the difference is greater than the array's length 
    diff = diff - quotelen; 
    }  
    
    // The quote of the day 
    var todayQuote = quote[diff]; 
    onload = function(){document.getElementById('quote').firstChild.data = todayQuote}  
    // how to add Authors to the Quotes? ... getElementById(' Name ').firstChild.data = todayName ? 
    </script> 
    </head>  
    <body>  
    <div id = 'quote'>&nbsp;</div>  
    

    任何幫助,這將是偉大的。 歡呼聲。

    +2

    我不明白你想要的最終結果是什麼。你能用簡單的英語解釋理想的結果嗎? – RobertAKARobin

    +0

    作者隱藏在哪裏? – sideroxylon

    +0

    此外,'diff - quotelen'不一定會給你一個數組中的索引。你需要得到模數:'dif%quotelen' - 餘數將在0和數組長度之間。 – sideroxylon

    回答

    0

    而不是將quotes作爲字符串推送,您可以推送一個對象。也可以使用%運算符代替循環來獲得差異。

    { quote: 'quote 1 Lorem ipsum blah blah' , aurthor: 'foo1' }, 
    

    function getTodaysQuote(quote) { 
     
        var firstDate = new Date(2016, 0, 1); 
     
        var today = new Date(); 
     
    
     
        // Difference in days 
     
        var index = Math.floor((+today - +firstDate)/(1000 * 60 * 60 * 24)) % quote.length; 
     
        return quote[index] 
     
    } 
     
    
     
    function printQuote(quote) { 
     
        document.querySelector('.quote').innerHTML = quote.quote; 
     
        document.querySelector('.author').innerHTML = "-" + quote.author; 
     
    } 
     
    
     
    function main() { 
     
        var quotes = [ 
     
        { quote: 'quote 1 Lorem ipsum blah blah' , author: 'foo1' }, 
     
        { quote: 'quote 2 Nullam commodo blah blah' , author: 'foo2' }, 
     
        { quote: 'quote 3 Sed vitae blah blah' , author: 'foo3' }, 
     
        { quote: 'quote 4 Maecenas blah blah' , author: 'foo4' }, 
     
        { quote: 'quote 5 Fusce lorem velit, blah blah' , author: 'foo5' }, 
     
        { quote: 'quote 6 Ut blah blah' , author: 'foo6' }, 
     
        { quote: 'quote 7 Phasellus blah blah', author: 'foo7' }, 
     
        ] 
     
    
     
        var q = getTodaysQuote(quotes); 
     
        printQuote(q); 
     
    } 
     
    main();
    .quote { 
     
        font-size: 20px; 
     
        font-weight: bold; 
     
        color: #333; 
     
        margin: 0; 
     
    } 
     
    .author { 
     
        float: right; 
     
        margin:0; 
     
    } 
     
    .content{ 
     
        width: 50%; 
     
    }
    <div class="content"> 
     
        Todays quote is: 
     
        <p class="quote"></p> 
     
        <p class="author"></p> 
     
    </div>

    +0

    不行,對不起還沒有工作。 – Gav

    +0

    @Gav當你說*它不適合我*,請指定什麼不工作。如果可能,分享一個工作樣本,以便每個人都可以幫助你。 – Rajesh

    +0

    我試着把代碼放在腳本標籤裏面,而CSS裏面的樣式標籤(和你一樣),但是沒有任何東西顯示在頁面上。我應該替換'document.write'的'print'嗎? – Gav

    相關問題