JavaScript在文本框中默认显示图片背景,获得焦点后消失的代码
效果演示:http://css-tricks.com/examples/InputWithBackgroundImage/
html代码
<form name="searchform" id="search-form"> <div> <b>Search</b> <input type="text" name="txtInput" title="Enter the terms you wish to search for." /> <input type="submit" value="GO!" class="submit" style="cursor: pointer;" /> </div> </form>
JS代码
<script type="text/javascript" language="javascript"> (function() { var id = document.getElementById('search-form'); if (id && id.txtInput) { var name = id.txtInput; var unclicked = function() { if (name.value == '') { name.style.background = '#FFFFFF url(images/googbg.png) left no-repeat'; } }; var clicked = function() { name.style.background = '#ffffff'; }; name.onfocus = clicked; name.onblur = unclicked; unclicked(); } })(); </script>