![](/img/trans.png)
[英]$window.focus() doesn't work on existing tabs in iOS Safari
[英]iOS Safari - Give focus to window
我有這個jsFiddle ,它有一個簡單的div
和一個 jQuery 點擊處理程序:
<div class="testDiv"
data-target="http://www.stackoverflow.com"
data-open="false">
</div>
$(document).ready(function(){
var newWindow;
$('.testDiv').on('click', function(e){
var target = $(this).data('target');
var open = $(this).data('open');
if (!open){
$(this).data('open','true');
newWindow = window.open(target);
}
else {
if (newWindow.closed){
newWindow = window.open(target);
}
newWindow.focus();
}
});
})
div
上的點擊處理程序旨在打開一個新的 window(如果它尚未打開); 如果它是打開的,它會給 window 焦點。 This works fine in Chrome on Win 7 x64 but on iOS 8.3's Safari (on an iPhone 6), once the window is open, clicking the div
doesn't switch focus to the opened window. 您必須關閉 window 並再次打開它才能獲得焦點。
此 iOS Safari 焦點問題的任何解決方案?
這對我有用,在 iOS(平板電腦)、MacOS(M1)、Chrome 上測試:
const link = 'http://...';
const target = 'NEW_WINDOW'
let win = window.open(link, target);
if (win && !win.closed) {
win.close();
}
win = window.open(link, target);
if (win) {
setTimeout(() => win.focus(), 100);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.