Help us understand the problem. What is going on with this article?

querySelectorの使い方

querySelector

JavaScriptには「getElementById()」とか「getElemetnsByClassName()」などHTML要素を取得できるメソッドはありますが、

「querySelector()」を使うとid属性値・class属性値などを意識せずにjQuery感覚でHTML要素をセレクタ指定することができます。

index.html
<p>全ての色が混ざり合うと白くなるものは何か?</p>
<button onclick="result1()">答え</button>
<p id="ans1">ここに答えを表示</p>
index.js
let list1 = document.querySelector('#ans1');
function result1() {
  list1.innerHTML = '<h4>加法混色</h4>'+'<img src="img/rgb.png">';
}

スクリーンショット 2020-10-27 8.18.18.png

kyototea
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away