我希望能夠爲第一個頁面視圖顯示大型促銷,然後才顯示可擴展的最小化促銷。看來,我在JavaScript條件不會改變divs的風格。警報對話框(放置用於測試)確實有效,cookie看起來正在運行。我是一個JavaScript新手。Javascript Cookie用於更改divs的顯示
任何幫助表示讚賞。
<script>
if (document.cookie.indexOf('visited=true') == -1) {
var oneDay = 1000*60*60*24*1;
var expires = new Date((new Date()).valueOf() + oneDay);
document.cookie = "visited=true;expires=" + expires.toUTCString();
alert('We can show the large promo because this is the first page view for today.');
} else {
alert('We\'ve already shown the large promo, we will show a much smaller expandable promo.');
document.getElementById('promo').style.display='none';
document.getElementById('promo-reduced').style.display='block';
}
</script>
<div id="promo-reduced" style="display:none;">
<a onclick="show();"> <img src="/images/small_icon.gif" height="26" width="48" border="0" align="left" />Learn about this product.</a>
</div>
<div id="promo" style="display:block;">
<a onclick="conceal();">Close</a>
<a href="/learn-more.html" target="_blank" onclick="conceal();"><img src="/images/large-promo.jpg" border="0" /></a><br />
</div>
腳本在HTML放置在頁面上之前執行。您應該將上面的腳本放在HTML代碼下面。我希望你在'document.onload'上執行腳本 – Panther