0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScript 特定の要素の中身と背景色を変更する方法

Posted at

#JavaScript 備忘録

###1.特定の要素の中身をごっそり書き換える方法

書き換えたい対象要素のinnerHTMLプロパティに文字列を代入すると、「対称の要素」の中身が「置き換える内容」に変化する。
実際に使う場合は、書き換えたい対称の要素にはid属性を使ってid名を割り振っておき、getElementByIdメソッドを使って特定し、innerHTMLプロパティに文字列を代入すれば良い。

(対象の要素).innerHTML = "置き換える内容"

 document.getElementByID("my_color_p").innerHTML = "This is Red!";


2.任意の要素の背景色を動的に変更する方法

getElementByIdを使って対象要素を特定し、styleプロパティのbackgroundColorプロパティの値に、望みの背景色を代入するだけ

document.getElementById('対象id名').style.backgroundColor = '新しい背景色';


document.getElementById("my_color").style.backgroundColor = 'red;

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?