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秒
![[トップページ]](../lib/img//top.gif)
![[一覧]](../lib/img//list.gif)
![[検索]](../lib/img//search.gif)
![[ヘルプ]](../lib/img//help.gif)
![[ログイン]](../lib/img//login.gif)