我在网页上有一个文本框,它有一个默认值,当我点以下它变空。
怎样用js实现,怎样调用?
还有一个问题,服务器控件与html控件有何不同,啥时用?

解决方案 »

  1.   

    <input type="text" value="aaa" onclick="this.value=''">
    服务器控件asp.net在服务器端使用的控件,html控件就是传统的客户端控件!
      

  2.   

    单独用onclick="this.value=''"> 不好,请用下面语句
    onclick="if(this.value='默认值'){this.value=''}"
      

  3.   

    楼上两位更好,我是多此一举,但还是贴上来吧:
    <script language="javascript" type="text/javascript">
        function SetInputNull() {
            var input = document.getElementById("txtInput");
            input.value = "";
        }
    </script><input id="txtInput" type="text" value="请输入内容!" onclick="SetInputNull();" />
      

  4.   

    如果你要用<asp:textbox>的话就把这个textbox放在一个<Div>里面,然后定义Div的onclick事件的js处理函数
      

  5.   


    onfocus="if(this.value='默认值'){this.value=''}" //获得焦点,如果直接用点击的话,用键盘移过来焦点的就不能清空了
      

  6.   

    onfocus="if(this.value="aa"){this.value=''}"
    或者
    onfocus="if(this.value="")this.value='aa' onblur="if(this.value='aa'){this.value=''}"
      

  7.   

    <.....onfocus="if(this.value==this.defaultValue) this.value='';" onblur="if(this.value=='') this.value=this.defaultValue;"...
      

  8.   

    不能用onclick,用onfocus
    <input type="text" id="text1" onfocus="if(this.value='默认值'){this.value=''}"/>第二个问题:服务器控件的优点是:就算浏览器禁用了js,服务器也会在后台执行。不过这个例子里无法体现。
    通常是:如果要验证一个控件是否是空,空了就不允许提交,
    普通html控件只能靠js验证,然后后台再将提交后页面的控件值验证一遍,
    如果是服务器控件,直接在cs文件里写段验证,前台后台都生成验证代码。
      

  9.   

    越到后面高手也越多,都再等抛砖引玉吗,呵呵
    觉得还是onfocus比较实用