0

みんな私はこれに夢中になっていて、javascriptを使用する制約がたくさんあることを知っていますが、私が構築しているサイトにはhtmlファイルしかありません。phpも使用できますが、参照できるが含まれていない別のファイルである必要があります。最終的な拡張子は .html にする必要があります

そうは言っても、日付を含む画像名を持つ画像のディレクトリを取得するスクリプトの写真を作成しています。たとえば、今日の今日の画像は20120822.jpgで、年末までの膨大な画像ディレクトリがあります。明日の画像は20120823.jpgで、昨日は20120821.jpg です

その部分は簡単でした..しかし、今は以前に表示されたすべての画像から構成されたギャラリーを作成したい..現在の日付のファイル名に至るすべての画像. したがって、ディレクトリを読み取るか、現在のイメージの日付ファイル名につながる最後の 20 個のイメージを生成する必要があります。前の 20 枚の画像か何かで解決します。

ここに私がその日の写真に使用しているコードがあります..どうすればいいですか?

    <script>


     //returns the current date in YYYYMMDDf_01.jpg format
     function getCurrentDateString(){

       //make a new date object set at the current date
       var currentDate = new Date();

       //get the four position year and
       //make it a string
       var currentYear = currentDate.getFullYear() + "";

       //get the zero-indexed month and add 1
       //for the real month and make it a strintg
       var currentMonth = (currentDate.getMonth() + 1) + "";

       //add a zero at the beginning if only one
       //digit month
       if (currentMonth.length == 1) {
         currentMonth = "0" + currentMonth;
       }

       //get the current day of the month and
       //make it a string
       var currentDayOfMonth = currentDate.getDate() + "";

       //add a zero if necessary
       if (currentDayOfMonth.length == 1) {
         currentDayOfMonth = "0" + currentDayOfMonth;
       }

       return(currentYear + currentMonth + currentDayOfMonth);
     }

     //preload image based upon currentDate
     var currentDateString = getCurrentDateString();

     var dailyImageObject = new Image();

      var dailyImageObjectURL = new Image();

     dailyImageObject.src = "http://perillotours.com/galleries/photo-of-the-day/images/" +         currentDateString + ".jpg";

     dailyImageObjectURL.href = "http://perillotours.com/galleries/photo-of-the-day/images/" + currentDateString + ".jpg";

     //called when the page loads to
     //set the actual HTML IMG element to have the same
     //SRC as our cached Image object
     function showDailyImage(){
       document.getElementById("dailyIMGElement").src = dailyImageObject.src;
     }
      function showDailyImageURL(){
       document.getElementById("dailyIMGElementURL").href = dailyImageObjectURL.href;
     }

     </script>

これは、その日の写真を表示するために HTML ページに配置するコードです。

    <a rel="prettyphoto" id="dailyIMGElementURL">
    <img width="523" style="background-color: #000000;" id="dailyIMGElement" height="335" border="0" /></a> 

したがって、基本的には、最後の 20 枚の画像で上記のコードを 20 回ループしたいと考えています。私は本当にそれを行う方法を考えることはできません..

ヘルプやアイデアをお寄せいただきありがとうございます。

イアン

4

1 に答える 1

0

コードは jQuery を参照していないため、jQuery を使用する場合は次のようになります。

  1. ループから開始 - このようにして、必要な画像の数を簡単に定義できます。日付オブジェクトを使用すると、何日も減算することを心配する必要はありません。(作業例: http://jsfiddle.net/qH8xX/2/ )

    var html = "";
    for( i = 0; i < 20; i++ ) {
        html += "<li>" + i + "</li>";
    }
    
    $("#gallery").html( html );
    
  2. ループで、日付から 1 を減算し、日付として保存します。(作業例: http://jsfiddle.net/qH8xX/3/ )

    var html = "";
    var date = new Date(); // store the current date
    
    for( i = 0; i < 20; i++ ) {
        // subtract 1 from date
        // more here: http://stackoverflow.com/questions/1187824/finding-date-by-subtracting-x-number-of-days-from-a-particular-date-in-javascrip
        date = new Date( date.setDate( date.getDate() - 1 ) );
        html += "<li>" + i + " = " + date.toDateString() + "</li>";
    }
    
    $("#gallery").html( html );
    

3. あなたの番: 日付オブジェクトを使用して、正しいファイル名でイメージ タグを作成します。これを使用すると役立ちます: http://www.w3schools.com/jsref/jsref_obj_date.asp

于 2012-08-22T16:02:34.057 に答える