gpt4 book ai didi

javascript - 在单页 pushState Web 应用程序中模拟画外音页面加载

转载 作者:技术小花猫 更新时间:2023-10-29 10:53:04 24 4
gpt4 key购买 nike

我正在开发单页应用程序 (SPA),我们正在使用 HTML 5 模拟多页应用程序 history.pushState .它在视觉上看起来不错,但在 iOS 画外音中表现不正确。 (我假设它在任何屏幕阅读器中都不起作用,但画外音是我首先尝试的。)

这是我试图实现的行为示例。下面是两个普通的网页:

1.html

<!DOCTYPE html><html>
<body>This is page 1. <a href=2.html>Click here for page 2.</a></body>
</html>

2.html

<!DOCTYPE html><html>
<body>This is page 2. <a href=1.html>Click here for page 1.</a></body>
</html>

漂亮又简单。画外音是这样读的:

Web page loaded. This is page 1.
[swipe right] Click here for page 2. Link.
[double tap] Web page loaded. This is page 2.
[swipe right] Click here for page 1. Visited. Link.
[double tap] Web page loaded. This is page 1.

这里又是一个单页应用程序,使用历史操作来模拟实际页面加载。

spa1.html

<!DOCTYPE html><html>
<body>This is page 1.
<a href='spa2.html'>Click here for page 2.</a></body>
<script src="switchPage.js"></script>
</html>

spa2.html

<!DOCTYPE html><html>
<body>This is page 2.
<a href='spa1.html'>Click here for page 1.</a></body>
<script src="switchPage.js"></script>
</html>

switchPage.js

console.log("load");
attachHandler();

function attachHandler() {
document.querySelector('a').onclick = function(event) {
event.preventDefault();
history.pushState(null, null, this.href);
drawPage();
}
}

function drawPage() {
var page, otherPage;
// in real life, we'd do an AJAX call here or something
if (/spa1.html$/.test(window.location.pathname)) {
page = 1;
otherPage = 2;
} else {
page = 2;
otherPage = 1;
}
document.body.innerHTML = "This is page " + page +
".\n<a href='spa"+otherPage+".html'>Click here for page " +
otherPage + ".</a>";
attachHandler();
}

window.onpopstate = function() {
drawPage();
};

(请注意,此示例不适用于文件系统;您必须从网络服务器加载它。)

这个 SPA 示例在视觉上看起来与简单的多页面示例完全一样,除了第 2 页“加载速度更快”(因为它根本没有真正加载;这一切都在 JS 中发生)。

但在 Voiceover 中,它并没有做正确的事情。

Web page loaded. This is page 1.
[swipe right] Click here for page 2. Link.
[double tap] Click here for page 1. Visited. Link.
[The focus is on the link! swipe left] This is page 2.
[swipe right] Click here for page 1. Visited. Link.
[double tap] Web page loaded. This is page 1.

焦点在链接上,而它本应位于页面顶部。

我如何告诉 Voiceover 整个页面刚刚更新,以便读者应该从页面顶部继续阅读?

最佳答案

Jorgen's answer , 这是基于 another StackOverflow thread让我走上了正确的轨道。

实际的修复不是将整个页面包装在 <div tabindex=-1> 中而是创建一个 <span tabindex=-1>仅围绕第一部分(“这是第 N 页”)并重点关注。

function drawPage() {
var page, otherPage;
// in real life, we'd do an AJAX call here or something
if (/spa1.html$/.test(window.location.pathname)) {
page = 1;
otherPage = 2;
} else {
page = 2;
otherPage = 1;
}
document.body.innerHTML = '<span tabindex="-1" id="page' + page + '">This is page ' + page +
'.</span>\n<a href="spa'+otherPage+'.html">Click here for page ' +
otherPage + '.</a>';

document.getElementById('page' + page).focus();
setTimeout(function() {
document.getElementById('page' + page).blur();
}, 0);
attachHandler();
}

请注意,在此示例中,我们还在超时中模糊了焦点;否则,非屏幕阅读器浏览器将在跨度周围绘制一个可见的蓝色焦点矩形,这不是我们想要的。模糊焦点不会影响 iOS VO 阅读器的焦点。

关于javascript - 在单页 pushState Web 应用程序中模拟画外音页面加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38402443/

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