Window focus() 方法
实例
Assure that the new window GETS focus (send the new window to the front):
<!DOCTYPE html>
<html>
<body>
<p>Click the button to open a new window, and assure that the new window GETS focus (send the new window to the front).</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var myWindow = window.open("", "", "width=200,height=100");
myWindow.document.write("<p>A new window!</p>");
myWindow.focus();
}
</script>
</body>
</html>
More “Try it Yourself” examples below.
定义与用法
The focus() method sets focus to the current window.Tip: Use theblur() method to remove focus from the current window.Note: This method makes a request to bring the current window to the foreground. It may not work as you expect in all browsers, due to different user settings.
Browser Support
The numbers in the table specify the first browser version that fully supports the method.
Method | |||||
---|---|---|---|---|---|
focus() | Yes | Yes | Yes | Yes | Yes |
语法
window.focus()
参数
None |
返回值
No return value |
更多实例
实例
Assure that the new window does NOT get focus (send the new window to the background):
<!DOCTYPE html>
<html>
<body>
<p>Click the button to open a new window, and assure that the new window does NOT GET focus (send the new window to the background).</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var myWindow = window.open("", "", "width=200, height=100");
myWindow.document.write("<p>A new window!</p>");
myWindow.blur();
}
</script>
</body>
</html>