getBGCOLOR()
レイヤ−の背景色を調べるサンプルファンクションです。
n4では
bgColorへ、それ以外は
backgroundColorを読んでいます。
n6,m1とe5以降のstyleのプロパティはインラインに書き込まれていない限り初期値が空白になりますので、
ここのサンプルではあらかじめonload時に色をセットしてあります。
*このファンクションをコピーして<script>と</script>の間にペーストしておくだけでこの機能をクロスブラウザに利用できるようになります。
Cross-Browser のための Sample Function
//--レイヤ−名で処理する場合
Syntax : getBGCOLOR('レイヤ−名')
function getBGCOLOR(layName,color){
if(document.getElementById) //e5,e6,n6,n7,m1,o6,o7,s1用
return document.getElementById(layName).style.backgroundColor
else if(document.all) //e4用
return document.all(layName).style.backgroundColor
else if(document.layers) //n4用
return document.layers[layName].bgColor
}
Example
<script type='text/javascript'>
<!--
function getBGCOLOR(layName,color){
if(document.getElementById) //e5,e6,n6,n7,m1,o6,o7,s1用
return document.getElementById(layName).style.backgroundColor
else if(document.all) //e4用
return document.all(layName).style.backgroundColor
else if(document.layers) //n4用
return document.layers[layName].bgColor
}
function setBGCOLOR(layName,color){
//opera6 は透明が効かないのでページ背景色と同色(ここではwhite)へ便宜修正
if(color=='')(navigator.userAgent.search("Opera(\ |\/)6")!= -1)
?color='white':color='transparent'; //←このwhiteを背景色に書換える
if(document.getElementById) //e5,e6,n6,n7,m1,o6,o7,s1用
document.getElementById(layName).style.backgroundColor =color
else if(document.all) //e4用
document.all(layName).style.backgroundColor=color
else if(document.layers){ //n4用
if(color=='transparent')color=null
document.layers[layName].bgColor=color
}
}
//-->
</script>
<body onLoad="setBGCOLOR('test','pink')">
<!--↓このボタンをクリックすると///////////////////////////-->
<form>
<input type="button" value="What is this color?"
onClick="alert(getBGCOLOR('test'))">
</form>
<!--↓このレイヤ−の背景色がわかります/////////////////////-->
<div id="test"
style="position:absolute;left:50px;top:50px;
width:400;height:200;clip:rect(0,400,200,0)">
このレイヤ−の背景色を調べます
</div>
//--オブジェクト名で処理する場合
上記の関数をレイヤー名の代わりにオブジェクトで処理するようにしたもの。
ブラウザ毎の実装オブジェクト処理が別立てになるので関数がシンプルになる。
Syntax : getBGCOLORoj(オブジェクト)
function getBGCOLORoj(oj){
if(document.getElementById ||
document.all) //e4,e5,e6,n6,n7,m1,o6,o7,s1用
return oj.backgroundColor
else if(document.layers) //n4用
return oj.bgColor
}
Example
<script type='text/javascript'>
<!--
function getBGCOLORoj(oj){
if(document.getElementById ||
document.all) //e4,e5,e6,n6,n7,m1,o6,o7,s1用
return oj.backgroundColor
else if(document.layers) //n4用
return oj.bgColor
}
function setBGCOLOR(layName,color){
//opera6 は透明が効かないのでページ背景色と同色(ここではwhite)へ便宜修正
if(color=='')(navigator.userAgent.search("Opera(\ |\/)6")!= -1)
?color='white':color='transparent'; //←このwhiteを背景色に書換える
if(document.getElementById) //e5,e6,n6,n7,m1,o6,o7,s1用
document.getElementById(layName).style.backgroundColor =color
else if(document.all) //e4用
document.all(layName).style.backgroundColor=color
else if(document.layers){ //n4用
if(color=='transparent')color=null
document.layers[layName].bgColor=color
}
}
//--layNameで指定したオブジェクトを返す(必ずonload後に実行すること)
function getLayStyleOj(layName){
if(document.getElementById) //e5,e6,n6,n7,m1,o6,o7,s1用
return document.getElementById(layName).style
else if(document.all) //e4用
return document.all(layName).style
else if(document.layers) //n4用
return document.layers[layName]
}
//-->
</script>
<body onLoad="setBGCOLOR('test','pink')">
<!--↓このボタンをクリックすると///////////////////////////-->
<form>
<input type="button" value="What is this color?"
onClick="alert(getBGCOLORoj(getLayStyleOj('test')))">
</form>
<!--↓このレイヤ−の背景色がわかります/////////////////////-->
<div id="test"
style="position:absolute;left:50px;top:50px;
width:400;height:200;clip:rect(0,400,200,0)">
このレイヤ−の背景色を調べます
</div>