青島のしま〜Blue Islands〜


JavaScript Tips

ドラッグされたことを判定する

    // 初期化関数
    function Init()
    {
        // マウスイベント用関数を登録
        document.onmousemove = OnMouseMove;
    }

    // マウスが動いたとき
    function OnMouseMove(e)
    {
        if (event.button == 1) { // 左クリックされているとき
            // 処理
        }
    }

HTMLを動的に書き換える

  • HTML中の要素をIDを指定して取得する:getElementById("id名")
  • innerHTMLプロパティを書き換えることで,指定した要素以下のHTMLを書き換えることができます.
  • innerTextプロパティを書き換えると,中身のテキストだけが変わります.
function Input() {
	// テキストエリアの文字を,下のdivエリアに反映させる
	var inputArea = document.getElementById("inputArea");
	var freeArea = document.getElementById("freeArea");
	freeArea.innerHTML = textAreaInput.innerText + ;

	// ボタンの文字を変更する
	var inputButton = document.getElementById("inputButton");
	inputButton.innerText = "押されたよ";
}
HTMLは以下のように
<body>
	<form>
		<textarea id="inputArea"  rows="10" cols="80"></textarea>
		<input id="inputButton" type="button" value="入力" onClick="javaScript:Input()"/>
	</form>
	<div id="freeArea"></div>
</body>

日時を取得する

function AddDate() {
	// 日時を取得 (yyyy/mm/dd hh:mm:ssの形式)
	var now = new Date();
	var nowString = now.getYear() + "/" + (now.getMonth()+1) + "/" + now.getDate() + " " 
					+ now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
	// HTMLに追加
	//textarea.innerHTML = "現在の時刻は" + nowString + "です";
}

メッセージボックスを出す

function Hello World() {
	// 単純なメッセージボックスの表示
	alert("Hello World!");
	// OK/Cancelのメッセージボックスの表示
	var result = confirm("よろしいですか?");
	if (result == true) {
		// OKのときの処理
	}
}


作成日: 2006年04月22日09時07日12秒
更新履歴
2008年07月22日 2008年02月08日 2008年02月06日 2008年02月05日 2007年10月25日 2006年12月20日 2006年12月19日 2006年12月05日 2006年09月20日 2006年06月18日 2006年06月12日 2006年04月22日 2006年03月30日 2006年02月28日 2006年02月23日 2006年02月18日 2006年01月13日 2005年12月21日