[基礎][JavaScript]変数を理解する05
メッセージを表示するダイアログボックス
ソースコード【 JavaScript 】
alert( メッセージ );
ユーザーに確認を求めるダイアログボックス
ソースコード【 JavaScript 】
confirm( メッセージ ); 変数 = confirm( メッセージ );
-
- confirmメソッドは「戻り値を返す」
-
- ブーリアン型「true(真)」または「false(偽)」
ユーザーにデータを入力してもらうダイアログボックス
ソースコード【 JavaScript 】
prompt( メッセージ ); 変数 = prompt( メッセージ, デフォルトの値 );
文字列を数値に変換
- 入力された数字が文字列ではなく、数値として認識させる
ソースコード【 JavaScript 】
parseInt( );
ソースコード【 HTML 】
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>標準体重</title> </head> <body> <script> /* *標準体重計算プログラム *最終修正日:2012.06.07 */ var height; //身長 var weight; //体重 //身長を入力する height=prompt('身長を入力してください',170); height=parseInt(height); //計算を行う weight=(height-100)*0.9; //結果を表示する document.write('<h1>'); document.write('身長が',height,'cmの人の標準体重は'); document.write(weight,'kgです。'); document.write('<\/h1>'); </script> </body> </html>
ブラウザで表示
ソースコード【 HTML 】
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>標準体重</title> </head> <body> <script> var height;//身長 var weight;//体重 var man;//男性かどうか //男性か女性かを入力 man=confirm('あなたは男性ですか?'); //身長を代入する height=prompt('身長を入力してください',180); height=parseInt(height); //計算を行う if(man){ weight=(height-80)*0.7; }else{ weight=(height-70)*0.6; } //結果を表示する document.write('<h1>'); document.write('身長が',height,'cmの'); if(man){ document.write('男性の標準体重は'); }else{ document.write('女性の標準体重は'); } document.write(weight,'kgです。'); document.write('<\/h1>'); </script> </body> </html>
ブラウザで表示