---
title: "获得焦点时,文本框内值隐藏，失去焦点时，文本框内的值显示"
author: "PBFox"
date: "2007-11-17T09:21:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2007-11-17T09:21:00Z"
id: 479252
url: "https://developerwork.iteye.com/blog/479252"
---

javascript代码：  
function clearText(thefield){  
if (Element.readAttribute(thefield, "defaultvalue")==thefield.value){  
thefield.value = ""  
thefield.style.color="black"  
}  
}

function refill(thefield){  
if(thefield.value==""){  
thefield.value=Element.readAttribute(thefield, "defaultvalue")  
thefield.style.color="#ccc"  
}  
}

ruby代码：

&lt;label for="search\_query"&gt;Search&lt;/label&gt;  
&lt;%= text\_field\_tag 'q', "Search", {:id=&gt;"search\_query", :class=&gt;"text\_field", :defaultValue=&gt;"Search",:onfocus=&gt;'clearText(this)', :style=&gt;'color:#ccc;', :onblur=&gt;'refill(this)'} %&gt;