如题:有一个input;<input type="text" value="默认"/>
当未选中input时,input显示默认值“默认”,并且颜色为灰色,当选中input后,删除默认值"默认",并且在此输入文字后,颜色为黑色。

解决方案 »

  1.   


    <!doctype html>
    <html>
    <head>
    <meta charset="gb2312" />
    <style>

    </style>
    </head>
    <body>
    <input id="test" type="text" value="默认" style="color:#999;" />
    <script>
    function $(o){return document.getElementById(o)}
    $('test').onfocus = function(){
    if( this.value == this.defaultValue ){
    this.value = '';
    this.style.color = '#000';
    }
    }
    </script>
    </body>
    </html>
      

  2.   

    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript">
    window.onload = function() {
    document.getElementById('demo').onclick = function() {
    var obj = this.getElementsByTagName('input')[0];
    obj.value = '';
    obj.disabled = false;
    obj.focus();
    }
    }
    </script>
    </head><body>
    <span id="demo"><input type="text" value="默认" disabled="disabled"  /></span>
    </body>
    </html>
      

  3.   

    您好! 这样写,当焦点选中这个input时,并没有把默认的值删掉
      

  4.   

    怎么没删除?
    又添加了 失焦的<!doctype html>
    <html>
        <head>
            <meta charset="gb2312" />
            <style>
                
            </style>
        </head>
        <body>
            <input id="test" type="text" value="默认" style="color:#999;" />
            <script>
                function $(o){return document.getElementById(o)}
                $('test').onfocus = function(){
                    if( this.value == this.defaultValue ){
                        this.value = '';
                        this.style.color = '#000';
                    }
                }
                $('test').onblur = function(){
                    if( this.value == '' || this.value == this.defaultValue ){
                        this.value = this.defaultValue;
    this.style.color = '#999';
                    }
                }
            </script>
        </body>
    </html>直接copy代码 另存为 ××.html 双击就可以了.
      

  5.   

    因为,浏览器的问题,this,不支持,所以导致不能删除,直接拷贝放到html,用IE打开,的确是起作用了
      

  6.   

    什么浏览器,不支持this
    ?
      

  7.   

    行业特性,自己公司的浏览器,基于webkit,但并没有完全继承,是一个阉割版的浏览器