たとえば、 のボタンがありbackground-color:#FF0000
、そのプロパティ (色) を変数に格納したいとします。私はこれを試しました:
var x=document.getElementById("button1").style.background;
しかし、それは機能しません。
間違えてすみません、私はピリオドを使用しています。訂正しました。
ここに完全なコードのスニペットがあります:-
<html>
<body>
<button id="button1" style="width:200px;height:200px;background-color:#00FF00;font-color:white" type="button" onclick="colorchange()">this is green</button>
<button id="button2" style="width:200px;height:200px;background-color:#FF0000;font-color:white" type="button" onclick="colorchange()">this is red</button>
<script>
function colorchange()
{
var color1;
var color2;
var color3;
color1=document.getElementById("button1").style.background
color2=document.getElementById("button2").style.background
color3=color1;
color1=color2;
color2=color3;
document.getElementById("button1").style.background=color1
document.getElementById("button2").style.background=color2
}
</script>
</body>
</html>
問題は、背景色を変数に保存できないように見えることです。