1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8">
    5. <title></title>
    6. </head>
    7. <style>
    8. input{
    9. color: #999;
    10. }
    11. </style>
    12. <body>
    13. <input type="text" value="手机" />
    14. <script>
    15. /*
    16. * 当鼠标点击文本框时,里面的默认文字会隐藏,当鼠标离开文本框时,里面的文字显示。
    17. * 案例分析:
    18. * (1)首先表单需要2个事件:获得焦点onfocus,失去焦点onblur
    19. * (2)如果获得焦点,判断表单里面内容是否为默认文字,如果是,就清空表单内容
    20. * (3)如果失去焦点,判断表单内容是否为空,如果为空,就将表单内容还原为默认文字
    21. */
    22. //获取事件源
    23. var text=document.querySelector("input");
    24. //注册事件,获得焦点事件 onfocus
    25. text.onfocus=function(){
    26. if(text.value=="手机"){
    27. text.value=" ";
    28. }
    29. text.style.color="#333";//获得焦点需要把输入的文字颜色加深
    30. }
    31. //注册事件,失去焦点事件 onblur
    32. text.onblur=function(){
    33. if(text.value==" "){
    34. text.value="手机";
    35. }
    36. text.style.color="#999";//失去焦点,需要把文本框里面的文字颜色变浅
    37. }
    38. </script>
    39. </body>
    40. </html>