function txtonfocus() {
         var txt = document.getElementById("txtSearch");
         txt.vaklue = "123";   
     }
我在文本框得到焦点时触发这个方法. 为什么不可以呢??

解决方案 »

  1.   

    txt.vaklue = "123";   
    ============================
    txt.value="123";
      

  2.   

    txt.vaklue = "123";  
    value写错了
      

  3.   


    额. 补好意思. 是打错了. 
    txt.value = "123" 这样子也不行.
      

  4.   

    是我在这里打快了没留意..txt.value = "123";
    这样子也不行.
      

  5.   

     function txtonfocus() {         document.getElementById("txtSearch").innerText = "123";   
         }
      

  6.   


    <head runat="server">
        <title>无标题页</title>
        <script type="text/ecmascript">
        function txtonfocus()
        {
           var t=document.getElementById("txtSearch");
           t.value="123";
        }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <input type=text id="txtSearch" onfocus="txtonfocus()" />
        </div>
        </form>
    </body>
    </html>
      

  7.   


    我需要用asp.net 的TextBox控件....
      

  8.   

       function txtonfocus() {
             var txt = document.getElementById("txtSearch").value;
              
         }
      

  9.   


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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 runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
        </div>
        </form>
        <script>
            document.getElementById("<%= txtSearch.ClientID %>").onfocus = function () {
                this.value = "123";
            }
        </script>
    </body>
    </html>
      

  10.   

     document.getElementById("<%= txtSearch.ClientID %>").onfocus = function () {
                this.value = "123";
            }这个是可以的。asp.net服务器端控件在客户端的ID变了。你分别
    alert( document.getElementById("txtSearch").length);
    alert( document.getElementById("<%= txtSearch.ClientID %>").length);
    看看。
      

  11.   


    <head runat="server">
        <title>无标题页</title>
        <script type="text/ecmascript">
        function txtonfocus()
        {
           var t=document.getElementById("<%=this.txtSearch.ClientID%>");
           t.value="123";
        }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox></div>
        </form>
    </body>
    </html>using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.txtSearch.Attributes.Add("onfocus", "txtonfocus()");
        }
    }
      

  12.   


    <head runat="server">
        <title>无标题页</title>
        <script type="text/ecmascript">
        function txtonfocus()
        {
           var t=document.getElementById("<%=this.txtSearch.ClientID%>");
           t.value="123";
        }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox></div>
        </form>
    </body>
    </html>using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.txtSearch.Attributes.Add("onfocus", "txtonfocus()");
        }
    }
      

  13.   

    测试过的,11楼的方法可行。<script language="javascript" type="text/javascript">
        function txtOnfocus() {
             document.getElementById("txt1").onfocus = function () { this.value = "123"; };
         }
    </script>
    <input type="text" name="txt1"  id="txt1"/>
      

  14.   


    document.getElementById("txtSearch").value;
      

  15.   

    <script language="javascript" type="text/javascript">
        function txtOnfocus() {
            document.getElementById("<%=this.TextBox1.ClientID %>").onfocus = function () { this.value = "123"; };
         }
    </script>
    <input type="text" name="txt1"  id="txt1"/>
      

  16.   


    <asp:TextBox ID="TextBox1" Name="111" runat="server" onfocus="txtOnfocus()"></asp:TextBox>
      

  17.   

      function txtonfocus()
        {
           var t=document.getElementById("<%=this.txtSearch.ClientID%>");
           t.value="123";
        }
      

  18.   

    终于又有一个人懂了 欣慰 为什么学习。net前都没先学下控件原理呢,都不看看他怎么来的,变成了什么呢。