我正在製作一種腳本,每天用不同的圖像和標題顯示不同的內容。我已經有了圖像循環來顯示每天不同的圖片,但我需要幫助才能對標題做同樣的處理。如何每天使用javascript更改文本內容?
它是爲了製作像每天不同的折扣優惠的動態頁面,但一週後重復,因此我只需要顯示7張圖片和7個標題。
這裏是我的代碼,每天只有圖像的循環。
var dailyPhotos;
var today, img;
dailyPhotos = function() {
today = new Date();
weekday = today.getDay();
showImages = [ ];
myPhotos = [ "{{root}}assets/img/sunday.jpg", "{{root}}assets/img/monday.jpg", "{{root}}assets/img/tuesday.jpg", "{{root}}assets/img/wednesday.jpg", "{{root}}assets/img/thirsday.png", "{{root}}assets/img/friday.jpg", "{{root}}assets/img/saterday.jpg" ]; // You must specify the path or file name of your images that will be loaded in a weekday basis.
if (document.images) {
for (var x = 0; x < myPhotos.length; x++) {
showImages[ x ] = new Image();
showImages[ x ].src = myPhotos[ x ];
} img = ((document.getElementById) ? document.getElementById("yourImageId") : document.images.yourImageId); // Specify the id of the image that will get raplaced daily.
img.src = showImages[ weekday ].src;
img.alt = myPhotos[ weekday ];
} return false; // If the browser can't display images, then EXIT FUNCTION.
};
window.onload = dailyPhotos;
,這是我多麼希望在HTML
<div class="row">
<div class="column large-6 medium-6 small-12">
<h4> THIS IS WHERE I WANT THE TITLE TO BE DISPLAYED </h4>
<p> IF I CAN DISPLAY P TEKST WITH IT I WOULD HAVE IT IN HERE </p>
<a href="More details medium-6 small-12"></a>
</div>
<div class="column large-6"><img id="yourImageId" src="tuesdayPhoto.jpg" alt="DEMO" /> THIS IS WHERE THE IMAGE IS DISPLAYED </div>
</div>
,以顯示它,我知道它可以用JavaScript來完成,感謝試圖幫助,如果你正在讀這一點。
var dailyPhotos;
var today, img;
dailyPhotos = function() {
today = new Date();
weekday = today.getDay();
showImages = [ ];
myPhotos = [ "{{root}}assets/img/sunday.jpg", "{{root}}assets/img/monday.jpg", "{{root}}assets/img/tuesday.jpg", "{{root}}assets/img/wednesday.jpg", "http://s.cdpn.io/37045/wedding-1.jpg", "{{root}}assets/img/friday.jpg", "{{root}}assets/img/saterday.jpg" ]; // You must specify the path or file name of your images that will be loaded in a weekday basis.
if (document.images) {
for (var x = 0; x < myPhotos.length; x++) {
showImages[ x ] = new Image();
showImages[ x ].src = myPhotos[ x ];
} img = ((document.getElementById) ? document.getElementById("yourImageId") : document.images.yourImageId); // Specify the id of the image that will get raplaced daily.
img.src = showImages[ weekday ].src;
img.alt = myPhotos[ weekday ];
} return false; // If the browser can't display images, then EXIT FUNCTION.
};
window.onload = dailyPhotos;
<div class="row"> <div class="column large-6 medium-6 small-12"> <h4> THIS IS WHERE I WANT THE TITLE TO BE DISPLAYED </h4> <p> IF I CAN DISPLAY P TEKST WITH IT I WOULD HAVE IT IN HERE </p> <a href="More details medium-6 small-12"></a> </div> <div class="column large-6"> <img id="yourImageId" src="tuesdayPhoto.jpg" alt="DEMO" /> THIS IS WHERE THE IMAGE IS DISPLAhttp://stackoverflow.com/posts/43251456/edit#YED </div> </div>
請點擊'<>'代碼編輯器,並使用lorempixel創建[MCVE] – mplungjan
做到了,但你看不到圖像 –
https://dummyimage.com/300&text=Monday – mplungjan