[待解决问题] Js 求救????
提问时间: 2008-05-10 13:58
离问题结束还有 8 天 1 小时 浏览:108 次

   我想用Javascript实现,当页面加载完成后,让页面上的一个<input>自动获得焦点。并触发它的onfocus事件。

 


提问者:JackLee - 菜鸟二级

问题补充:问题已经解决:
<body onload="document.all['txt'].focus()">
   <input id=txt>
</body>

所有回答(3)
<body onload="document.getElementById('textbox1').focus();">
<input type="text" id="textbox1"/>

6天前   回答者:光光 - 初学一级
楼上的方法是一般方法。除此方法外还可以:一种是从后台代码向前台写Javascript,一种是直接在前台写上Javascript,具体方法如下:
第一种方法:
【后台CS页Page_Load事件代码】
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)//根据情况而定看是否需要此判断
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "plok", "<script language='javascript' type='text/javascript'>okonfocus();</script>");
        }
    }
}
【前台页面脚本代码如下】
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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>Input测试页</title>
    <script language="javascript" type="text/javascript">
    function okonfocus()
    {
    var myok = document.getElementById("OK");
    myok.value = "OK已经获得了焦点,并执行了okonfocus()函数!";
    myok.focus();
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input id="OK" type="text" onfocus="okonfocus();" style="width: 435px"/>
    </div>
    </form>
</body>
</html>

第二种方法:
【只有前台页面脚本代码如下】
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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>Input测试页</title>
    <script language="javascript" type="text/javascript">
    function okonfocus()
    {
    var myok = document.getElementById("OK");
    myok.value = "OK已经获得了焦点,并执行了okonfocus()函数!";
    myok.focus();
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input id="OK" type="text" onfocus="okonfocus();" style="width: 435px"/>
    </div>
    <script language="javascript" type="text/javascript">
    okonfocus();
    </script>
    </form>
</body>
</html>
6天前   回答者:刚刚 - 小虾三级
window.onload = function() { ... }
6天前   回答者:Elden - 菜鸟二级
6天前   光光 :
方法不错学习了。 不过第二种 好像多调用了次。


 ps. 我外婆也叫我 刚刚 呵呵
5天前   刚刚 :
呵呵,有趣,光光与刚刚,好难分哦!
第二种可以拆分JS代码,我只是举个例子!

   您需要登录以后才能回答!
我的问题    我要提问


快到期问题

> 问题排行榜

有不合适内容,建议去除