2017年4月7日金曜日

開発環境

Head First HTML5 Programming (Elisabeth Robson (著)、Eric Freeman (著)、O'Reilly Media)の Chapter 2.(Introducing JavaScript and the DOM: A Little Code)、Make decisions with JavaScript の SHARPEN YOUR PENCIL(No. 1635) を取り組んでみる。

SHARPEN YOUR PENCIL(No. 1635)

コード(Emacs)

HTML5

<button id="run0">run</button>
<button id="clear0">clear</button>

<h1>Temperatures</h1>
<ul>
  <li id="temp0"></li>
  <li id="temp1"></li>
  <li id="temp2"></li>
  <li id="temp3"></li>
  <li id="temp4"></li>
</ul>

<script src="sample4.js"></script>

JavaScript

let btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0');

let showTemps = () => {
    let tempByHour = [59.2, 60.1, 63, 65, 62];

    tempByHour.forEach((theTemp, i) => {
        let id = `temp${i}`;
        let li = document.querySelector(`#${id}`);
        if (i === 0) {
            li.textContent = `The temperature at noon was ${theTemp}.`;
        } else {
            li.textContent = `The temperature at ${i} was ${theTemp}.`;
        }
    });
};

btn0.onclick = showTemps;
btn1.onclick = () => {
    for (let i = 0; i < 5; i += 1) {
        let id = `temp${i}`;
        let li = document.querySelector(`#${id}`);
        
        li.textContent = '';
    }
};

showTemps();

Temperatures

0 コメント:

コメントを投稿

Comments on Google+: