2011-12-05

JavaScript: 文字欄位浮水印 轉自:伊沙熊的學習筆記本:

轉自:伊沙熊的學習筆記本:【備忘】JavaScript: 文字欄位浮水印 - 樂多日誌:
javasciprt程式碼:
<input name="textfield" type="text"  value="浮水印自訂文字" style="color: #AAAAAA;" onfocus="if (this.value == '浮水印自訂文字') {this.value = ''; this.style.color='#333333'}" onblur="if (this.value == '') {this.value = '浮水印自訂文字'; this.style.color='#AAAAAA'}" >

程式碼(AS3):
01// txtfield1 是要放浮水印的目標文字欄位
02txtfield1.addEventListener (FocusEvent.FOCUS_OUT, addTextDefault);
03txtfield1.addEventListener (FocusEvent.FOCUS_IN, removeTextDefault);
04
05var default_tf = txtfield1.getStyle ("textFormat");
06
07function addTextDefault (e) {
08 if (e.currentTarget.text == "") {
09 e.currentTarget.setStyle ("textFormat", new TextFormat(null,null,0xAAAAAA));
10 e.currentTarget.text = "←請輸入資料";
11 }
12}
13function removeTextDefault (e) {
14 if (e.currentTarget.text == "←請輸入資料") {
15 e.currentTarget.setStyle ("textFormat", default_tf);
16 e.currentTarget.text = "";
17 }
18}
19addTextDefault ({currentTarget:txtfield1});
很短的程式碼, setStyle函式是用來設定組件的樣式

沒有留言: