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

jQuery Mobile 安装

详解在 HTML 页面中引入 jQuery Mobile 1.4.5 的两种方式(CDN 加载与本地托管),核对 viewport 元标签、jQuery 先于 jQuery Mobile 加载的依赖顺序、HTML5 script 省略 type 属性规则及 HTTPS 迁移注意事项。

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

学完这一章,你能够

两种引入方式与 CDN 基本原理

在网页中启用 jQuery Mobile 有两种途径:通过 CDN(Content Delivery Network,内容分发网络)直接外链样式与脚本,或从官方仓库下载压缩包部署到自有服务器。CDN 通过边缘节点就近分发静态资源以降低延迟与提升可用性;无需在本地安装编译工具,只要在 `<head>` 中按顺序引入视口 `<meta name="viewport" content="width=device-width, initial-scale=1">`、jQuery Mobile CSS、jQuery 核心库与 jQuery Mobile JS 即可运行。

官方 CDN、国内镜像与历史协议排查

经典 1.4.5 配置使用官方 `code.jquery.com`(搭配 jQuery 1.11.3)或历史国内镜像 `apps.bdimg.com`(搭配 jQuery 1.10.2)。离线审查这些历史片段时需重点排查三点:第一,`jquery.min.js` 必须排在 `jquery.mobile-1.4.5.min.js` 之前,否则会触发 `$ is not defined`;第二,原文示例使用明文 `http://`,现代站点在 HTTPS 页面下会因混合内容被浏览器拦截,必须改为 HTTPS 或本地托管;第三,部分早期第三方公共 CDN 节点可能已停服,且 jQuery Mobile 本身已于 2021 年归档停更,新项目应使用原生 CSS 与现代打包工具。

本地托管与 HTML5 脚本标签简化

若项目处于内网或离线环境,可将 `jquery.mobile-1.4.5.css`、`jquery.js` 与 `jquery.mobile-1.4.5.js`(及配套 `images/` 图标目录)下载并置于站点同级或静态资源目录下以相对路径引用。按照 HTML5 规范,JavaScript 已是 `<script>` 标签的默认脚本语言,因此不再需要书写冗余的 `type="text/javascript"` 属性。

CDN 概念与托管选型提示

主题技术说明与现代部署提醒
CDN(Content Delivery Network)内容分发网络通过就近节点加速静态资源传输并减少链路瓶颈;由于 jQuery Mobile 已停更且历史 HTTP 公共节点存在失效或混合内容风险,维护旧系统建议自托管并启用 HTTPS。

HTML5 <script> 默认类型说明

主题规范依据
省略 type="text/javascript"HTML5 规定浏览器默认将 <script> 解析为 JavaScript,因此无需显式添加 type="text/javascript"(ES 模块则使用 type="module")。

运行 jQuery Mobile CDN 与本地托管头部依赖顺序离线核验

python · 阅读源码
"""Offline jQuery Mobile DOM & data-role verifier for Chapter 03: install."""
import json
from lxml import html

VERSION = "CPython 3.12.14 / lxml offline jQuery Mobile DOM & data-role verifier"


def inspect_head(head_markup: str) -> dict:
    doc = html.fromstring(f"<!DOCTYPE html><html>{head_markup}<body></body></html>")
    vp = doc.xpath("//head/meta[@name='viewport']/@content")
    css = doc.xpath("//head/link[@rel='stylesheet']/@href")
    scripts = doc.xpath("//head/script")
    srcs = [s.get("src", "") for s in scripts]
    types = [s.get("type") for s in scripts]
    jq_idx = next((i for i, u in enumerate(srcs) if "jquery" in u and "mobile" not in u), -1)
    jqm_idx = next((i for i, u in enumerate(srcs) if "jquery.mobile" in u or "jquerymobile" in u), -1)
    return {
        "has_viewport": bool(vp and "width=device-width" in vp[0] and "initial-scale=1" in vp[0]),
        "css_count": len(css),
        "script_count": len(srcs),
        "jquery_before_jqm": jq_idx != -1 and jqm_idx != -1 and jq_idx < jqm_idx,
        "omits_legacy_script_type": all(t is None for t in types),
    }


def run_cases():
    cases = []

    # Case 1: Official CDN head setup (jQuery 1.11.3 + jQuery Mobile 1.4.5)
    official_head = """
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
      <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
      <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    </head>
    """
    actual_1 = inspect_head(official_head)
    expected_1 = {
        "has_viewport": True,
        "css_count": 1,
        "script_count": 2,
        "jquery_before_jqm": True,
        "omits_legacy_script_type": True,
    }
    cases.append({
        "name": "official_cdn_load_order",
        "summary": "Verify viewport meta, stylesheet link, jQuery-before-jQuery-Mobile script order, and HTML5 script tag without type attribute.",
        "expected": expected_1,
        "actual": actual_1,
        "passed": actual_1 == expected_1,
    })

    # Case 2: Domestic mirror CDN head setup (historical Baidu CDN pattern)
    mirror_head = """
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">
      <script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
      <script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    </head>
    """
    mirror_info = inspect_head(mirror_head)
    mirror_doc = html.fromstring(f"<!DOCTYPE html><html>{mirror_head}<body></body></html>")
    http_urls = [u for u in mirror_doc.xpath("//link/@href | //script/@src") if u.startswith("http://")]
    actual_2 = {
        **mirror_info,
        "legacy_http_url_count": len(http_urls),
    }
    expected_2 = {
        "has_viewport": True,
        "css_count": 1,
        "script_count": 2,
        "jquery_before_jqm": True,
        "omits_legacy_script_type": True,
        "legacy_http_url_count": 3,
    }
    cases.append({
        "name": "mirror_cdn_and_http_audit",
        "summary": "Verify mirror CDN dependency order and detect legacy cleartext http:// URLs requiring HTTPS migration.",
        "expected": expected_2,
        "actual": actual_2,
        "passed": actual_2 == expected_2,
    })

    # Case 3: Local self-hosted relative asset setup
    local_head = """
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="jquery.mobile-1.4.5.css">
      <script src="jquery.js"></script>
      <script src="jquery.mobile-1.4.5.js"></script>
    </head>
    """
    local_info = inspect_head(local_head)
    local_doc = html.fromstring(f"<!DOCTYPE html><html>{local_head}<body></body></html>")
    rel_paths = local_doc.xpath("//link/@href | //script/@src")
    actual_3 = {
        **local_info,
        "all_relative_same_dir": all("://" not in p and "/" not in p for p in rel_paths),
        "files": rel_paths,
    }
    expected_3 = {
        "has_viewport": True,
        "css_count": 1,
        "script_count": 2,
        "jquery_before_jqm": True,
        "omits_legacy_script_type": True,
        "all_relative_same_dir": True,
        "files": ["jquery.mobile-1.4.5.css", "jquery.js", "jquery.mobile-1.4.5.js"],
    }
    cases.append({
        "name": "local_self_hosted_assets",
        "summary": "Verify local same-directory CSS and JS references with jQuery loaded before jQuery Mobile.",
        "expected": expected_3,
        "actual": actual_3,
        "passed": actual_3 == expected_3,
    })

    return cases


def main():
    print(json.dumps({"version": VERSION, "cases": run_cases()}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • 确认三种 <head> 配置均包含 viewport 元标签、样式表、jQuery 先于 jQuery Mobile 的脚本顺序、省略旧 type 属性,并识别出历史镜像的 3 处 http:// 明文链接。
运行 jQuery Mobile CDN 与本地托管头部依赖顺序离线核验

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

当前源码 SHA256:f5c3fa51448bbca2473887dcd8614efad586df23a6a26d2239189117e6feace9

下载本站示例源码

本地运行

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

下载jQuery Mobile 安装实验

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

ZIP SHA256:b2c801682b17c30d7d455e5016e9588e282d78387189d1ad988c13ecb6b97ae4

常见错误与边界

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

练习与解答

在引入 jQuery Mobile 时,如果把 jquery.mobile-1.4.5.min.js 放在 jquery-1.11.3.min.js 之前会发生什么问题?为什么必须配置 viewport 元标签?

展开参考解答

jQuery Mobile 依赖全局 jQuery 对象注册插件,先加载 jQuery Mobile 会因 jQuery 尚未定义而报错失效;viewport(width=device-width, initial-scale=1)则告诉移动浏览器按设备逻辑宽度渲染页面,避免默认缩放到桌面宽度导致字体与按钮过小。

规范与官方依据