本文執行個體講述了javascript搜尋方塊效果實現方法。分享給大家供大家參考。具體實現方法如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>搜尋方塊效果</title><script type="text/javascript">function iniEvent() { var txtSearch = document.getElementById("txtSearch"); txtSearch.onfocus = function () { if (this.value == "請輸入關鍵字" || this.value == "") { this.value = ""; this.style.color = "black"; //修改文字框字型顏色 } } txtSearch.onblur = function () { if (this.value == "請輸入關鍵字" || this.value == "") { this.value = "請輸入關鍵字"; this.style.color = "red"; //修改文字框字型顏色 } else { this.style.color = "black"; } }}</script></head><body onload="iniEvent()"><!--文字離開如果文字框為空白則顯示紅色的"請輸入關鍵字"--><!--<label for="txtSearch">尋找</label><input type="text" id="txtSearch" value="請輸入關鍵字" style="color:red" />-->尋找<input type="text" id="txtSearch" value="請輸入關鍵字" style="color:red" /><br /><input type="text" /></body></html>
希望本文所述對大家的javascript程式設計有所協助。