[pre]public static setFocus(newFocus:
Object) : Boolean
Object) : Boolean
使 newFocus 参数指定的可选择(可编辑)文本字段、按钮或影片剪辑获得焦点。如果传递 null 或 undefined,则删除当前焦点。
可用性
ActionScript 1.0、Flash Player 5 , 按钮和影片剪辑的实例名称仅在 Flash Player 6 及更高版本中有效。
参数
newFocus:Object― 一个对象(例如,某个按钮、影片剪辑或文本字段实例),或者是一个字符串,该字符串指定某个按钮、影片剪辑或文本字段实例的路径。如果您传递指定路径的字符串,请将该路径置于引号内 (" ")。可以使用点或斜杠记号指定路径。如果要使用 ActionScript 2.0,则必须使用点符号表示。可以使用相对路径或绝对路径。
返回
Boolean― 一个布尔值;如果获得焦点成功,则为 true;如果失败,则为 false。
示例
在下面的示例中,当文本字段在浏览器窗口中运行时,它将在 username_txt 文本字段上获得焦点。如果用户未填写某个必需文本字段(username_txt 和 password_txt),则光标将自动将焦点定位在缺少数据的文本字段中。例如,如果用户未在 username_txt 文本字段中键入任何内容,则在单击姨峤挥按钮时,将出现一条错误消息,而且光标在 username_txt 文本字段中获得焦点。
[pre]this.createTextField("status_txt", this.getNextHighestDepth(), 100, 70, 100, 22);this.createTextField("username_txt", this.getNextHighestDepth(), 100, 100, 100, 22);this.createTextField("password_txt", this.getNextHighestDepth(), 100, 130, 100, 22);this.createEmptyMovieClip("submit_mc", this.getNextHighestDepth());submit_mc.createTextField("submit_txt", this.getNextHighestDepth(), 100, 160, 100, 22);submit_mc.submit_txt.autoSize = "center";submit_mc.submit_txt.text = "Submit";submit_mc.submit_txt.border = true;submit_mc.onRelease = checkForm;username_txt.border = true;password_txt.border = true;username_txt.type = "input";password_txt.type = "input";password_txt.password = true;Selection.setFocus("username_txt");//function checkForm():Boolean {if (username_txt.text.length == 0) {status_txt.text = "fill in username";Selection.setFocus("username_txt");return false;}if (password_txt.text.length == 0) {status_txt.text = "fill in password";Selection.setFocus("password_txt");return false;}status_txt.text = "success!";Selection.setFocus(null);return true;}
此示例中使用的 MovieClip.getNextHighestDepth() 方法要求 Flash Player 7 或更高版本。如果您的 SWF 文件包括第 2 版的组件,请使用第 2 版组件的 DepthManager 类代替 MovieClip.getNextHighestDepth() 方法。
