实现的效果如淘宝首页搜索框所示。
<!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" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> <title>请输入关键字查询</title> <style type="text/css"> .default { position: absolute; top: 10px; left: 3px; padding: 0 8px; z-index: 1; color: #8B9096; cursor: text; font-size:14px; } </style> </head> <body> <label class="default" id="default" for="txt1">请输入关键字查询</label> <input type="text" name="" id="txt1" class="txt" value=""/> <script type="text/javascript"> var oTxt = document.getElementById(‘txt1‘); var oDefault = document.getElementById(‘default‘); var sOld = oDefault.innerHTML; oTxt.onfocus = function () { oDefault.style.color = ‘#ccc‘; } oTxt.onblur = function () { oDefault.style.color = ‘#8B9096‘; } oTxt.onpropertychange = oTxt.oninput = function () { if (oTxt.value.length > 0) { oDefault.innerHTML = ‘‘; } else { oDefault.innerHTML = sOld; } }; </script> </body> </html>
时间: 2024-12-16 18:44:58