日期:2014-05-16 浏览次数:20417 次
<input type="text" maxlength="100" name="schname" id="schname" autocomplete="off" class="input_text" value="请输产品名称!" />
<script language="JavaScript" type="text/javascript"> function addListener(element,e,fn){ if(element.addEventListener){ element.addEventListener(e,fn,false); } else { element.attachEvent("on" + e,fn); } } var values = document.getElementById("schname"); addListener(values,"click",function(){ values.value = ""; }) addListener(values,"blur",function(){ if (values.value ==''){values.value = "请输产品名称!";} }) </script>
addListener(values,"focus",function(){ if(values.value=="请输产品名称!")values.value = ""; } //focus比click事件好
------解决方案--------------------
把下面的代码放在.js文件中
function addListener(element,e,fn){ if(element.addEventListener){ element.addEventListener(e,fn,false); } else { element.attachEvent("on" + e,fn); } } var values = document.getElementById("schname"); // 追加的判断.... if ( values ) { addListener(values,"click",function(){ values.value = ""; }) addListener(values,"blur",function(){ if (values.value ==''){values.value = "请输产品名称!";} }) }