ウェブ学のすすめ

Study of Web Design & Programing

DOMで文字色を変える

プレビュー


スマートフォンの方はこちらからプレビューできます。
DOMで文字色を指定

コード

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>DOMで文字色を指定</title>
<!--[if lte IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!--[if lte IE 9]>
<script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
<![endif]-->
</head>
<body>
<h1 id="myid">DOMによる要素の指定</h1>

<script>
document.getElementById('myid').style.color = '#ff0000';

document.getElementById('myid').style.backgroundColor = '#eee';
</script>

</body>
</html>

ポイント

  • 要素.style.プロパティ = 値;
  • プロパティに「-」が入る場合は、取り除き次の単語の頭文字を大文字にして記述
    (例) background-color → backgroundColor