我在页面中嵌入了一个java applet。当我“点击我”链接获得焦点时,它在java脚本中调用方法“focusApplet”。此方法调用applet的“requestFocus()”方法。功能“focusApplet”正在成功执行。但焦点不在于MAC上所有浏览器的applet内部,包括safari 5,Firefox和chrome。但是同一段代码正在Windows平台上运行。我也尝试了“requestFocusInWindow()”方法,但它无法正常工作。请帮我摆脱这个问题。
<HTML>
<HEAD>
</HEAD>
<BODY>
<script type="text/javascript">
function sayhello(){
var name = document.getElementById('hellotext');
name.focus();
name.click();
name.value = "raman";
}
function focusMe() {
alert('Hello world!!');
document.getElementById('hellotext').focus();
}
function show() {
var name = document.getElementById('name');
var address = document.getElementById('address');
alert("Hello " + name.value + " Your Address is : " + address.value);// + address.value
}
function focusApplet(){
document.getElementsByName('mediamaster')[0].requestFocus();
}
</script>
<form>
Text : <input type="text" id="mytext" >
Address : <input type="text" id="address" >
<div >
<a id="medialink" href="#" onfocus="focusApplet();">Click Me</a>
</div>
<div>
<APPLET name="mediamaster" CODE="FirstApplet.class" WIDTH="200"
HEIGHT="200" name="FirstApplet" id="fA"></APPLET>
</div>
Name : <input type="text" id="hellotext"> Address : <input type="text">
<input type="reset">
<button value="Show" onclick="show()">show</button>
</form>
</BODY>
</HTML>
小程序代码:
import java.applet.Applet;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import netscape.javascript.JSObject;
import org.w3c.dom.html.HTMLDocument;
import com.sun.java.browser.dom.DOMAccessor;
import com.sun.java.browser.dom.DOMAction;
import com.sun.java.browser.dom.DOMService;
import com.sun.java.browser.dom.DOMUnsupportedException;
public class FirstApplet extends Applet {
private Button clear_button;
private Choice color_choices;
private Button button;
public void init() {
this.setFocusable(true);
this.setBackground(Color.green);
try {
service = DOMService.getService(this);
} catch (DOMUnsupportedException e3) {
e3.printStackTrace();
}
final JSObject win = JSObject.getWindow(this);
// Create a button and add it to the applet.
// Also, set the button's colors
clear_button = new Button("Clear");
clear_button.setForeground(Color.black);
clear_button.setBackground(Color.lightGray);
button = new Button("Push Me");
this.add(clear_button);
this.add(button);
// Create a menu of colors and add it to the applet.
// Also set the menus's colors and add a label.
color_choices = new Choice();
color_choices.addItem("black");
color_choices.addItem("red");
color_choices.addItem("yellow");
color_choices.addItem("green");
color_choices.setForeground(Color.black);
color_choices.setBackground(Color.lightGray);
this.add(new Label("Color: "));
this.add(color_choices);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("Hello World!!! Thread2");
win.eval("sayhello()");
}
});
}
}