如何在路由变化时,自动聚焦到主内容区域并朗读标题?
解读
国内 Joomla 项目普遍要过「无障碍合规」与「政府/国企采购评测」两道关。
面试时,考官想确认三件事:
- 你是否知道 Joomla 路由事件体系(从
Joomla\CMS\Router到模板渲染的完整链路)。 - 能否在不侵入核心、不破坏多模板机制的前提下,用「插件+模板覆盖」双通道完成需求。
- 是否具备「读屏友好」意识:焦点管理、语义标签、aria-live、朗读优先级。
答得太浅(只写 window.onload)会被判「前端小白」;答得太深(改核心路由类)会被判「破坏升级路径」。必须给出「可落地、可维护、可过测」的折中方案。
知识点
- Joomla 路由触发顺序
Router::build()→JApplicationWeb::route()→dispatch()→onAfterRoute→onAfterDispatch→ 模板index.php入口。 - 系统插件生命周期
插件组system最先加载,可监听onAfterRoute/onAfterDispatch;返回 void,只操作$doc与$app对象,不中断流程。 - 模板覆盖机制
在模板根目录新建html/plg_system_focusreader/focusreader.php,即可被JLayout自动检索,避免硬编码在插件里。 - 无障碍规范(国标 GB/T 37668-2019)
要求「路由切换后焦点自动移至主内容;标题须被朗读」。实现方式:- 主容器加
tabindex="-1",使其可编程聚焦但不出现在 Tab 序列。 - 使用
aria-live="polite"区域存放朗读文本,避免重复朗读整页。
- 主容器加
- 前端无冲突策略
使用 Joomla 自带behavior.core依赖的Joomla.getOptions/Joomla.Text做多语言,不额外引入第三方朗读库,减少安全审查阻力。
答案
分三步落地,全部文件可打包成安装包,一键安装、卸载干净。
第一步:新建系统插件 plg_system_focusreader
目录结构
plugins/system/focusreader/focusreader.php
plugins/system/focusreader/focusreader.xml
focusreader.xml 声明
<extension type="plugin" group="system" method="upgrade">
<name>PLG_SYSTEM_FOCUSREADER_XML</name>
<files>
<filename plugin="focusreader">focusreader.php</filename>
<folder>tmpl</folder>
</files>
<languages>
<language tag="zh-CN">zh-CN.plg_system_focusreader.ini</language>
</languages>
</extension>
focusreader.php 核心代码
class PlgSystemFocusreader extends CMSPlugin
{
public function onAfterDispatch()
{
$app = $this->getApplication();
$doc = $app->getDocument();
// 只处理前台 HTML 输出
if ($app->isClient('administrator') || $doc->getType() !== 'html') {
return;
}
// 取当前文章/菜单项标题,多语言安全转义
$title = htmlspecialchars($doc->getTitle(), ENT_QUOTES, 'UTF-8');
// 把参数注入 Joomla 前端全局对象
$options = [
'title' => $title,
'contentId' => 'maincontent', // 与模板约定
'liveRegion' => 'liveregion'
];
$doc->addScriptOptions('plg_system_focusreader', $options);
// 注册脚本,依赖 Joomla core
$doc->getWebAssetManager()->registerAndUseScript(
'plg.system.focusreader',
'media/plg_system_focusreader/focusreader.min.js',
[],
['defer' => true],
['core']
);
}
}
第二步:前端脚本 media/plg_system_focusreader/focusreader.js
(function(Joomla, window) {
'use strict';
// 监听 Joomla 自定义路由事件,4.x 内置,3.x 用 polyfill
document.addEventListener('joomla:route', handleRouteChange);
// 兜底监听 popstate,兼容直接后退
window.addEventListener('popstate', handleRouteChange);
function handleRouteChange() {
const cfg = Joomla.getOptions('plg_system_focusreader');
if (!cfg) return;
const content = document.getElementById(cfg.contentId);
const live = document.getElementById(cfg.liveRegion);
if (content) {
content.setAttribute('tabindex', '-1');
content.focus({preventScroll:false});
}
if (live) {
live.setAttribute('aria-live', 'polite');
live.textContent = cfg.title; // 读屏软件自动朗读
}
}
})(Joomla, window);
第三步:模板级约定(可写入模板安装包说明)
在模板 index.php 主容器加 id:
<main id="maincontent" class="main" role="main">
<jdoc:include type="component" />
</main>
<div id="liveregion" class="sr-only" aria-live="polite"></div>
.sr-only 用 Bootstrap 通用样式,宽度高度 1px、隐藏但可读屏。
安装后,无论 SEF 如何开关、是否开启缓存,路由变化均触发聚焦与朗读,满足国内政采评测「无障碍」得分点,且插件卸载即失效,不留痕迹。
拓展思考
- 若站点使用 Vue、React 局部渲染,如何复用同一套 live region?
可把liveregion提为 Web Component,注册到window.Joomla.liveregion,无论哪套框架更新路由,只需调用Joomla.liveregion.speak('新标题')即可统一朗读,避免重复造轮子。 - 大型门户模板往往把主内容拆成多个模块,标题层级深,如何精准提取「当前页面最语义化标题」?
可在插件里监听onBeforeCompileHead,利用JDocumentRendererHead已计算好的$doc->title,或解析当前active->params中的page_heading,保证与浏览器标签一致。 - 未来 Joomla 5 将默认开启「核心 Web Vitals」审计,聚焦滚动可能触发 CLS 扣分。
可在focus({preventScroll:true})后,手动content.scrollIntoView({block:'start', behavior:'smooth'}),既保证读屏,又不产生布局偏移,面试时主动提及此细节,可体现性能敏感度。