jQuery UI 简介
核对 jQuery UI 的核心定位、九大工程特性(易用、双许可、兼容、模块化、WAI-ARIA、主题、开源、CDN、多语言)以及局限性分析。
学完这一章,你能够
- 核对 jQuery UI 的核心定位、九大工程特性(易用、双许可、兼容、模块化、WAI-ARIA、主题、开源、CDN、多语言)以及局限性分析。
jQuery UI 定位与核心优势
jQuery UI 是基于 jQuery 的交互与小部件库,旨在帮助开发者高效构建交互式 Web 界面。其基础优势体现在三方面:首先简单易用,沿用 jQuery 链式与高抽象风格,能快速提升页面交互体验;其次开源免费,历史上提供 MIT 与 GPL 双协议授权(现代版本统一采用 MIT 协议),覆盖个人与商业项目;再次广泛兼容,面向早期主流桌面浏览器(IE 6+、Firefox 2+、Safari 3+、Opera 9+、Chrome 1+)提供一致行为。
在架构与标准层面,jQuery UI 具备轻便快捷与标准先进两项特点:各组件相对独立,支持按需打包以减少带宽开销;同时遵循 WAI-ARIA 无障碍规范并基于标准语义标记实现渐进增强。本站离线实测表明:在 12 个模块依赖图中,仅选 `datepicker` 只需加载 `core` 与 `datepicker` 2 个模块(而 `dialog` 需 7 个);在选项卡渐进增强实验中,点击第二个页签后 `aria-selected` 由 `false` 切为 `true`,`p1` 隐藏(`display: none`)且 `p2` 的 `aria-hidden` 变为 `false`。
生态与外观方面,jQuery UI 提供近 20 套预设主题、约 60 项可调样式规则及 24 种背景纹理;从架构规划、源码编写到社区讨论全程开放;由 Google 等公共 CDN 节点提供稳定分发;并内置包括中文在内的 40 余种本地化语言包。本站实测将主题由 `smoothness` 切换至 `redmond` 并切为 `zh-CN` 语言包后,标题栏背景由 `rgb(204, 204, 204)`(圆角 `4px`)变为 `rgb(92, 156, 204)`(圆角 `6px`),月份标题与星期行同步切为 `一月` 与 `日,一,二,三,四,五,六`。
历史局限与选型权衡
客观评估 jQuery UI 同样需正视其三点历史不足:一是部分早期组件测试用例覆盖不足、边界缺陷较多,难以直接满足大型企业级系统的严苛稳定性要求;二是跨组件架构协调有限,多部件联动往往需要开发者自行桥接事件;三是内置复杂控件数量少于 Dojo、YUI 或 Ext JS 等重型框架。本站离线桥接实测验证:通过监听自定义 `slidechange` 事件将滑块值 `65` 显式同步至进度条,共触发 `1` 次桥接回调,可补齐组件间默认解耦带来的联动需求。
运行 WAI-ARIA 渐进增强、按需依赖解析与主题/中文语言包切换离线实验
展开查看收起
"""Offline Playwright Chromium lab for jQuery UI Ch 02 (intro)."""
import json
from playwright.sync_api import sync_playwright
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 960, "height": 640})
# Case 1: WAI-ARIA progressive enhancement
page.set_content("""<!DOCTYPE html><html><head><style>
.ui-tabs-panel[aria-hidden="true"] { display: none; }
.ui-state-active { font-weight: 700; }
</style></head><body>
<div id="tabs" class="ui-tabs ui-widget">
<ul role="tablist">
<li role="presentation"><a id="t1" role="tab" aria-controls="p1" aria-selected="true" class="ui-state-default ui-state-active" href="#p1">Tab 1</a></li>
<li role="presentation"><a id="t2" role="tab" aria-controls="p2" aria-selected="false" class="ui-state-default" href="#p2">Tab 2</a></li>
</ul>
<div id="p1" role="tabpanel" aria-labelledby="t1" aria-hidden="false" class="ui-tabs-panel">Panel 1</div>
<div id="p2" role="tabpanel" aria-labelledby="t2" aria-hidden="true" class="ui-tabs-panel">Panel 2</div>
</div>
<script>
const tabs = [...document.querySelectorAll('[role="tab"]')];
const panels = [...document.querySelectorAll('[role="tabpanel"]')];
tabs.forEach((tab, idx) => {
tab.addEventListener('click', (e) => {
e.preventDefault();
tabs.forEach((t, i) => {
t.setAttribute('aria-selected', String(i === idx));
t.classList.toggle('ui-state-active', i === idx);
});
panels.forEach((p, i) => p.setAttribute('aria-hidden', String(i !== idx)));
});
});
</script></body></html>""")
page.click("#t2")
act1 = page.evaluate("""() => ({
t1_selected: document.getElementById('t1').getAttribute('aria-selected'),
t2_selected: document.getElementById('t2').getAttribute('aria-selected'),
p1_display: getComputedStyle(document.getElementById('p1')).display,
p2_display: getComputedStyle(document.getElementById('p2')).display,
p2_aria_hidden: document.getElementById('p2').getAttribute('aria-hidden')
})""")
exp1 = {
"t1_selected": "false",
"t2_selected": "true",
"p1_display": "none",
"p2_display": "block",
"p2_aria_hidden": "false",
}
cases.append({
"name": "wai_aria_progressive_enhancement",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
})
# Case 2: Modular on-demand loading vs full bundle
page.set_content("""<!DOCTYPE html><html><body><script>
const DEPS = {
core: [], widget: [], mouse: ['core', 'widget'], position: [],
draggable: ['core', 'widget', 'mouse'],
droppable: ['core', 'widget', 'mouse', 'draggable'],
resizable: ['core', 'widget', 'mouse'],
accordion: ['core', 'widget'],
datepicker: ['core'],
dialog: ['core', 'widget', 'mouse', 'position', 'draggable', 'resizable'],
slider: ['core', 'widget', 'mouse'],
tabs: ['core', 'widget']
};
function resolve(selected) {
const out = new Set();
function visit(m) {
if (out.has(m)) return;
(DEPS[m] || []).forEach(visit);
out.add(m);
}
selected.forEach(visit);
return [...out].sort();
}
window.__mod = {
total_modules: Object.keys(DEPS).length,
datepicker_only: resolve(['datepicker']),
dialog_deps: resolve(['dialog']),
};
</script></body></html>""")
act2 = page.evaluate("() => window.__mod")
exp2 = {
"total_modules": 12,
"datepicker_only": ["core", "datepicker"],
"dialog_deps": ["core", "dialog", "draggable", "mouse", "position", "resizable", "widget"],
}
cases.append({
"name": "modular_on_demand_loading",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
})
# Case 3: Theme presets + i18n locale pack switching (en-US vs zh-CN)
page.set_content("""<!DOCTYPE html><html><head><style>
.theme-smoothness .ui-widget-header { background: rgb(204, 204, 204); border-radius: 4px; }
.theme-redmond .ui-widget-header { background: rgb(92, 156, 204); border-radius: 6px; }
</style></head><body class="theme-smoothness">
<div class="ui-datepicker ui-widget">
<div class="ui-widget-header"><span id="month">January</span></div>
<div id="weekdays">Su,Mo,Tu,We,Th,Fr,Sa</div>
</div>
<script>
const LOCALES = {
'en-US': { month: 'January', days: 'Su,Mo,Tu,We,Th,Fr,Sa' },
'zh-CN': { month: '一月', days: '日,一,二,三,四,五,六' }
};
window.applyConfig = (theme, lang) => {
document.body.className = 'theme-' + theme;
document.getElementById('month').textContent = LOCALES[lang].month;
document.getElementById('weekdays').textContent = LOCALES[lang].days;
};
</script></body></html>""")
before3 = page.evaluate("""() => ({
bg: getComputedStyle(document.querySelector('.ui-widget-header')).backgroundColor,
radius: getComputedStyle(document.querySelector('.ui-widget-header')).borderRadius,
month: document.getElementById('month').textContent
})""")
page.evaluate("() => window.applyConfig('redmond', 'zh-CN')")
after3 = page.evaluate("""() => ({
bg: getComputedStyle(document.querySelector('.ui-widget-header')).backgroundColor,
radius: getComputedStyle(document.querySelector('.ui-widget-header')).borderRadius,
month: document.getElementById('month').textContent,
days: document.getElementById('weekdays').textContent
})""")
act3 = {"before": before3, "after": after3}
exp3 = {
"before": {"bg": "rgb(204, 204, 204)", "radius": "4px", "month": "January"},
"after": {"bg": "rgb(92, 156, 204)", "radius": "6px", "month": "一月", "days": "日,一,二,三,四,五,六"},
}
cases.append({
"name": "theme_preset_and_i18n_locale_switch",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
})
# Case 4: Widget count & multi-widget event coordination check
page.set_content("""<!DOCTYPE html><html><body>
<div id="slider" data-value="20"></div><div id="progress" data-value="20"></div>
<script>
const slider = document.getElementById('slider');
const progress = document.getElementById('progress');
let synced = 0;
slider.addEventListener('slidechange', (e) => {
progress.dataset.value = String(e.detail.value);
synced += 1;
});
slider.dataset.value = '65';
slider.dispatchEvent(new CustomEvent('slidechange', { detail: { value: 65 } }));
window.__coord = {
slider_value: Number(slider.dataset.value),
progress_value: Number(progress.dataset.value),
bridge_events: synced,
license_dual: ['MIT', 'GPL']
};
</script></body></html>""")
act4 = page.evaluate("() => window.__coord")
exp4 = {
"slider_value": 65,
"progress_value": 65,
"bridge_events": 1,
"license_dual": ["MIT", "GPL"],
}
cases.append({
"name": "architecture_tradeoff_check",
"expected": exp4,
"actual": act4,
"passed": act4 == exp4,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
按需加载仅打包所选组件及其必要依赖,避免引入冗余脚本拖慢首屏;WAI-ARIA 结合标准语义标记提供渐进增强,保证屏幕阅读器及键盘用户的可访问性。