インラインJavaScript
<body>
<script>
document.write("Hello");
</script>
</body>
外部JavaScript
<script src="script.js"></script>
# script.js
var output1 = document.getElementById("output1);
var a = "Hello"
output1.innerHTML = y;
<body>
<p id=”output1”>Hello</p>
<script>
var output1 = document.getElementById(”output1”);
output1.innerHTML = “Bye”;
</script>
</body>
id
属性(識別子)をつけるdocument.getElementById()
で要素の取得を行い、変数に代入するinnerHTML
)に別の値を代入<p><input type=”text” id=”input1”></p>
<p><button onclick=”btn1();”>PUSH</button></p>
<p id=”output1”>出力</p>
<script>
var input1=document.getElementById(”input1”);
var output1=document.getElementById(”output1”);
function btn(){
output1.innerHTML=input1.value;
}
</script>
input
要素にid
属性をつけるonclick
属性:クリックしたら起動するプログラム・関数を定義function function_name(){}
:独自関数の定義input1.value
:入力の値text
:単一行のテキスト入力range
:スライダcolor
:色を指定するカラーピッカーdate
:日付けのカレンダtime
:時刻入力number
:数値入力tel
:電話番号テンキー(スマホのみ)value
:初期値入力max
:最大値min
:最小値step
:増分placeholder
:入力のヒント<p>Size
<select id=”input1”>
<option>S</option>
<option>M</option>
<option>L</option>
</select>
<button onclick=”btn1()”>PUSH</button>
</p>
<p id=”output1”>Choice</p>
<p>Message
<textarea id=”input1”></textarea>
</p>
<button onclock=”btn1()”>PUSH</button>
<pre id=”output1”>Result</pre>
Math.PI
:円周率(約3.14)Math.E
:ネイピア数(自然対数の底e=約2.718)Math.floor()
:切り捨てMath.round()
:四捨五入Math.ceil()
:切り上げMath.pow()
:累乗Math.random()
:乱数var now = new Date();
now.getFullYear()
:年now.getMonth()
:月+1now.getDate()
:日now.getDay()
:曜日now.getHours()
:時now.getMinutes()
:分now.getSeconds()
:秒now.getTime()
:UNIXエボック(1970.01.01 00:00:00からのミリ秒)