javascriptで季節によって、HTMLのテーブル背景を自動的に変えるスクリプトを教えてください。

<条件>
・コピペで使えるもの
・HTMLソース内書く
お願いいたします。

回答の条件
  • URL必須
  • 1人2回まで
  • 登録:
  • 終了:--
※ 有料アンケート・ポイント付き質問機能は2023年2月28日に終了しました。

回答5件)

id:sqrt No.1

回答回数40ベストアンサー獲得回数0

ポイント10pt

必要な仕様を勘違いしているかもしれませんが……

背景画像を変える場合:

<script type=”text/javascript”>

onload = function(images, t, i, j) {

images = [’haru.jpg’,’natsu.jpg’,’aki.jpg’,’fuyu.jpg’]

t = document.getElementsByTagName(’TABLE’)

i = Math.floor(((new Date().getMonth()+10)%12)/3)

for (j = 0; j < t.length; ++j) {

t[j].style.backgroundImage = ’url(’+images[i]+’)’

}

}

</script>

背景色を変える場合:

<script type=”text/javascript”>

onload = function(colors, t, i, j) {

colors = [’#fee’,’#efe’,’#ffe’,’#eef’] // 春色, 夏色, 秋色, 冬色

t = document.getElementsByTagName(’TABLE’)

i = Math.floor(((new Date().getMonth()+10)%12)/3)

for (j = 0; j < t.length; ++j) {

t[j].style.backgroundColor = colors[i]

}

}

</script>

id:maq No.2

回答回数81ベストアンサー獲得回数5

ポイント10pt

<SCRIPT type=”text/javascript”><!--

now=new Date();

mo=now.getMonth()+1;

if (mo==12 || mo<=2) {

document.write(’<TABLE BGCOLOR=”#0055FF”>’);

}

else if (mo>=3 && mo<=5) {

document.write(’<TABLE BGCOLOR=”#FF8FFF”>’);

}

else if (mo>=6 && mo<=8) {

document.write(’<TABLE BGCOLOR=”#00FF00”>’);

}

else {

document.write(’<TABLE BGCOLOR=”#0080FF”>’);

}

// --></SCRIPT>

<TR>

<TD>

TEST

</TD>

</TR>

</TABLE>

色etc.適当ですが。

id:sssplus No.3

回答回数29ベストアンサー獲得回数0

ポイント10pt

<script language=”javascript”>

<!--//

function ChangeTableBack()

{

var arrUrl = new Array(”URL-01.jpg”,”URL-02.jpg”,”URL-03.jpg”)

nTemp = Math.round(Math.random()*(arrUrl.length-1));

strHtml = ”url(”+arrUrl[nTemp]+”)”;

document.getElementById(”TableBack”).style.background = strHtml;

document.getElementById(”TableBack”).style.backgroundRepeat = ”no-repeat”;

}

//-->

</script>

のように書いてみてください。

「”URL-01.jpg”,”URL-02.jpg”,”URL-03.jpg”」

の部分に背景画像を指定します。

「document.getElementById(”TableBack”).style.backgroundRepeat = ”no-repeat”;」

の行はリピートさせないための記述です。

リピートさせる時は省略してかまいません。

id:ykan

このスクリプトは季節でかわるのでしょうか?

2004/05/01 12:35:52
id:kuippa No.4

回答回数1030ベストアンサー獲得回数13

ポイント10pt

時刻による選択のロジックを、月を抽出して比較するという微妙な変換が必要かもしれませんが、比較的サンプルの多いものです。

掲題URL1番目の背景自動切換えを見てみてください

2番目以降は参考に使えるページです。

http://www.red.oit-net.jp/tatsuya/java/

イヌでもわかるJavaScript講座

id:YUYUKOALA No.5

回答回数72ベストアンサー獲得回数0

ポイント50pt

<SCRIPT languqge=javascript>

<!--

//季節ごと(月ごと)にテーブル背景を変えるjavascript

//日付判定

now=new Date();

month=now.getMonth()+1;

//季節判定

var color;

//12月又は1〜2月

if (month==12 || month<=2) color=”silver”;

//3〜5月

if (month>=3 && month<=5) color=”pink”;

//6〜8月

if (month>=6 && month<=8) color=”aqua”;

//9〜11月

if (month>=9 && month<=11) color=”orange”;

//TABLEタブ記述開始

document.write(”<TABLE”);

//背景色指定

document.write(” bgcolor=”,color);

//その他オプション指定(省略可)

document.write(” border=1”);

//TABLEタブ記述終了

document.write(”>”);

//ここまで

// -->

</SCRIPT>

<!--以下通常のhtmlを記述-->

<TR>

<TD>TEST</TD>

<TD>TEST</TD>

<TD>TEST</TD>

</TR>

<TR>

<TD>TEST</TD>

<TD>TEST</TD>

<TD>TEST</TD>

</TR>

</TABLE>

---

色や月はお好きに設定してください

コメントはまだありません

この質問への反応(ブックマークコメント)

「あの人に答えてほしい」「この質問はあの人が答えられそう」というときに、回答リクエストを送ってみてましょう。

これ以上回答リクエストを送信することはできません。制限について

回答リクエストを送信したユーザーはいません