gpt4 book ai didi

javascript - window.focus() 和 window.blur() 有什么区别?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:12:24 24 4
gpt4 key购买 nike

我是 JavaScript 的新手...我无法找出 window.focus();window.blur( );

<!DOCTYPE html>
<html>
<body>
<button onclick="focus()">Click</button>
<script>
function focus() {
var myWindow = window.open("", "", "width=200,height=100");
myWindow.document.write("<p>A new window!</p>");
myWindow.focus();
}
</script>
</body>


当我使用它们时,我无法找到它们对窗口的任何操作....

帮我找出它们的用途...:)

最佳答案

它们基本相反:

window.focus 确保新窗口 GETS 获得焦点(将新窗口发送到最前面)。

window.blur 确保新窗口获得焦点(将新窗口发送到后台)。

示例:

-window.focus():

var myWindow = window.open("", "", "width=200, height=100"); // Opens a new window
myWindow.document.write("<p>A new window!</p>"); // Some text in the new window
myWindow.focus(); // Assures that the new window gets focus

-window.blur():

var myWindow = window.open("", "", "width=200, height=100"); // Opens a new window
myWindow.document.write("<p>A new window!</p>"); // Some text in the new window
myWindow.blur(); // Assures that the new window does NOT get focus

关于javascript - window.focus() 和 window.blur() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39158432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com