蓝色天空网mbsky.com · 知识 · 实践 · 创造jQuery UI · 本地阅读预览
教程 / 前端开发 / jQuery UI

jQuery UI 设计主题

核对自定义主题目录与分插件 CSS 文件命名规范、根类 `.ui-themename` 的启停与双主题共存作用域机制,以及基础样式表仅保留全局通用规则的组织原则。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

自定义主题目录结构与分插件命名规范

为了便于复用与维护,自定义主题应按固定目录结构组织:全部资源收拢在以主题名命名的独立文件夹 `themename/` 下;基础样式表 `themename/themename.css` 保持轻量且仅放全局核心规则,使用该主题的每个页面都必须引入;所支持的每个插件单独建立样式表 `themename/themename.pluginname.css`(例如为 `tabs` 编写时应为 `.css` 扩展名 `themename.tabs.css`,注意部分早期资料示例曾笔误写作 `themename.tabs.js`);图像资源如 `themename/img.png` 放于同目录且文件名可自由约定。实际完整结构可对照 jQuery UI 基本主题目录。本站离线校验器核对目录清单并确认 `.js` 扩展名不合法(`js_ext_is_css=false`)。

根类作用域定义与双主题局部覆盖

每个主题都应定义一个形如 `.ui-themename` 的根 CSS 类,借此可在 HTML 上一键启用或禁用主题,也支持在同一文档中并存多套主题。例如页面同时引入 `themename/themename.css`、`othertheme/othertheme.css` 与 `othertheme/othertheme.dialog.css`,给 `<body>` 挂载 `class="ui-themename"`,再给局部 `<div>` 挂载 `class="ui-othertheme"` 并内嵌 `.ui-dialog` 模态框:此时全页遵循 `themename` 风格,而 `.ui-othertheme` 容器及其内部对话框则受 `othertheme` 专属控制。本站离线实测该双主题页面:`<body>` 背景 `rgb(17, 17, 17)`、前景色 `rgb(255, 250, 250)`(`snow`),而局部 `.ui-othertheme` 背景切换为 `rgb(245, 247, 250)`、内部 `.ui-dialog` 背景 `rgb(255, 255, 255)` 且带 `rgb(0, 102, 204)` 边框。

基础文件 `themename.css` 仅应声明全局通用样式(如 `body.ui-themename` 的背景与前景色,以及 `.ui-themename a` 的常规、已访问和悬停链接色与 `outline: none`),不要混入特定插件的专有规则;多文件拆分在构建与下载阶段会自动合并精简,无需担心请求数过多。本站实测 `.ui-themename a` 常规色 `rgb(102, 136, 221)`(`#68D`)、`outline: none`,真实悬停后变为 `rgb(255, 255, 255)`(`#FFF`);移除 `body` 的 `ui-themename` 类后背景立刻回退为透明 `rgba(0, 0, 0, 0)`;同时验证 `othertheme.dialog.css` 仅作用于 `.ui-othertheme .ui-dialog`(内边距 `14px`),不污染外部未加作用域的 `.ui-dialog`(`0px`)。

运行主题目录校验、双主题根类局部覆盖、链接悬停与插件样式隔离离线实验

python · 阅读源码
"""Offline Playwright Chromium lab for jQuery UI Ch 10 (theme-design)."""
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")


DUAL_THEME_HTML = """<!DOCTYPE html>
<html>
<head>
<title>My Site</title>
<style id="css-base" data-href="themename/themename.css">
body.ui-themename { background: #111; color: snow; }
.ui-themename a, a.ui-themename { color: #68D; outline: none; }
.ui-themename a:visited, a.ui-themename:visited { color: #D66; }
.ui-themename a:hover, a.ui-themename:hover { color: #FFF; }
</style>
<style id="css-other" data-href="othertheme/othertheme.css">
.ui-othertheme { background: rgb(245, 247, 250); color: rgb(30, 30, 30); padding: 12px; }
.ui-othertheme a { color: rgb(0, 102, 204); }
</style>
<style id="css-other-dialog" data-href="othertheme/othertheme.dialog.css">
.ui-othertheme .ui-dialog { border: 2px solid rgb(0, 102, 204); background: rgb(255, 255, 255); color: rgb(20, 20, 20); padding: 14px; }
</style>
</head>
<body class="ui-themename">
  <p id="mainText">Main themed text <a id="mainLink" href="#page">Page Link</a></p>
  <div id="otherWrap" class="ui-othertheme">
    <div id="modalDlg" class="ui-dialog">This is a modal dialog.</div>
    <a id="otherLink" href="#dlg">Dialog Link</a>
  </div>
  <div id="outerDlg" class="ui-dialog">Unscoped dialog</div>
</body>
</html>"""


def main():
    cases = []
    with sync_playwright() as p:
        browser = launch_browser(p)
        page = browser.new_page(viewport={"width": 960, "height": 640})

        # Case 1: Theme directory & plugin CSS naming convention
        page.set_content("""<!DOCTYPE html><html><body><script>
const files = [
  'themename/themename.css',
  'themename/themename.tabs.css',
  'themename/themename.dialog.css',
  'themename/img.png'
];
const validCss = files.filter(f => f.endsWith('.css'));
const typoCheck = 'themename.tabs.js'.endsWith('.css');
window.__dir = {
  folder: 'themename/',
  base_css: 'themename/themename.css',
  plugin_css_files: validCss.filter(f => f !== 'themename/themename.css'),
  image_file: 'themename/img.png',
  js_ext_is_css: typoCheck
};
</script></body></html>""")
        act1 = page.evaluate("() => window.__dir")
        exp1 = {
            "folder": "themename/",
            "base_css": "themename/themename.css",
            "plugin_css_files": ["themename/themename.tabs.css", "themename/themename.dialog.css"],
            "image_file": "themename/img.png",
            "js_ext_is_css": False,
        }
        cases.append({
            "name": "theme_directory_structure_convention",
            "expected": exp1,
            "actual": act1,
            "passed": act1 == exp1,
        })

        # Case 2: Dual-theme root class scoping (.ui-themename on body + .ui-othertheme subtree)
        page.set_content(DUAL_THEME_HTML)
        act2 = page.evaluate("""() => ({
          body_bg: getComputedStyle(document.body).backgroundColor,
          body_color: getComputedStyle(document.body).color,
          other_wrap_bg: getComputedStyle(document.getElementById('otherWrap')).backgroundColor,
          modal_dlg_bg: getComputedStyle(document.getElementById('modalDlg')).backgroundColor,
          modal_dlg_border: getComputedStyle(document.getElementById('modalDlg')).borderTopColor,
          other_link_color: getComputedStyle(document.getElementById('otherLink')).color
        })""")
        exp2 = {
            "body_bg": "rgb(17, 17, 17)",
            "body_color": "rgb(255, 250, 250)",
            "other_wrap_bg": "rgb(245, 247, 250)",
            "modal_dlg_bg": "rgb(255, 255, 255)",
            "modal_dlg_border": "rgb(0, 102, 204)",
            "other_link_color": "rgb(0, 102, 204)",
        }
        cases.append({
            "name": "dual_theme_root_class_scoping",
            "expected": exp2,
            "actual": act2,
            "passed": act2 == exp2,
        })

        # Case 3: Base themename.css link states (:hover) and root-class enable/disable toggle
        normal_link = page.evaluate("""() => ({
          color: getComputedStyle(document.getElementById('mainLink')).color,
          outlineStyle: getComputedStyle(document.getElementById('mainLink')).outlineStyle
        })""")
        page.hover("#mainLink")
        hover_link = page.evaluate("() => getComputedStyle(document.getElementById('mainLink')).color")
        page.evaluate("() => document.body.classList.remove('ui-themename')")
        disabled_body_bg = page.evaluate("() => getComputedStyle(document.body).backgroundColor")
        act3 = {
            "link_normal_color": normal_link["color"],
            "link_outline": normal_link["outlineStyle"],
            "link_hover_color": hover_link,
            "body_bg_when_root_class_removed": disabled_body_bg,
        }
        exp3 = {
            "link_normal_color": "rgb(102, 136, 221)",
            "link_outline": "none",
            "link_hover_color": "rgb(255, 255, 255)",
            "body_bg_when_root_class_removed": "rgba(0, 0, 0, 0)",
        }
        cases.append({
            "name": "base_css_link_states_and_toggle",
            "expected": exp3,
            "actual": act3,
            "passed": act3 == exp3,
        })

        # Case 4: Plugin CSS isolation from base themename.css
        page.set_content(DUAL_THEME_HTML)
        act4 = page.evaluate("""() => ({
          scoped_dialog_padding: getComputedStyle(document.getElementById('modalDlg')).paddingTop,
          unscoped_dialog_padding: getComputedStyle(document.getElementById('outerDlg')).paddingTop,
          stylesheets_loaded: [...document.querySelectorAll('style[data-href]')].map(s => s.dataset.href)
        })""")
        exp4 = {
            "scoped_dialog_padding": "14px",
            "unscoped_dialog_padding": "0px",
            "stylesheets_loaded": [
                "themename/themename.css",
                "othertheme/othertheme.css",
                "othertheme/othertheme.dialog.css",
            ],
        }
        cases.append({
            "name": "plugin_css_isolation_from_base",
            "expected": exp4,
            "actual": act4,
            "passed": act4 == exp4,
        })

        browser.close()

    print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • 核对目录与插件 CSS 扩展名校验、body.ui-themename 与 .ui-othertheme .ui-dialog 双主题隔离、链接 #68D/#FFF 悬停变化及根类移除复原。
运行主题目录校验、双主题根类局部覆盖、链接悬停与插件样式隔离离线实验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

当前源码 SHA256:ef7dc77e5058edd4d675e881f6e5f0b7b4b7cbf238a93e9f69d394568bc26bab

下载本站示例源码

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载jQuery UI 设计主题实验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

ZIP SHA256:b956be861abc409f1f589622618e5b862c35d6deaba329d514e79e6019241849

常见错误与边界

  • 实际端口、文档资源与应用配置都应和测试条件一致。

练习与解答

在同时加载两套主题的页面里,为什么根类(如 `.ui-themename`、`.ui-othertheme`)能让局部对话框使用不同外观而不影响主页面?

展开参考解答

因为每套主题的样式规则都以各自的根类作为祖先选择器前缀,挂在 `<body>` 上的根类作用于全局,而内层 `<div>` 挂载另一主题根类后,凭借更近的上下文与专属插件选择器即可覆盖局部子树样式,且不会泄漏到容器之外。

规范与官方依据