护照信息辅助工具源代码

这个是谷歌AI Jemini帮我生成的一个简单网页小工具,保存下源代码,方便后续修改.

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>护照信息工具 - 模糊搜索与格式化</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f4f4f9;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            border-bottom: 2px solid #eee;
            padding-bottom: 10px;
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        input[type="text"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            border: 2px solid #007bff;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 18px;
            letter-spacing: 0.5px;
        }
        /* 格式化输入框的特定样式 */
        .format-group {
            border: 1px solid #ccc;
            padding: 15px;
            margin-bottom: 30px;
            border-radius: 6px;
            background-color: #f8f8f8;
        }
        .format-input-row {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        .format-input-row input {
            flex-grow: 1;
            margin-bottom: 0;
            border-color: #28a745; /* 绿色边框区分 */
        }
        #formattedOutput {
            flex-grow: 1;
            padding: 10px;
            border: 2px solid #007bff;
            border-radius: 4px;
            background-color: #e9ecef; /* 灰色背景表示只读 */
            font-size: 18px;
            color: #333;
            user-select: all; /* 方便用户复制 */
        }
        .format-group h2 {
            margin-top: 0;
            color: #28a745;
            border-bottom: 1px dashed #ddd;
            padding-bottom: 8px;
            margin-bottom: 10px;
        }
        
        /* 搜索结果表格样式 (保持不变) */
        .country-table-container {
            margin-top: 20px;
            max-height: 500px;
            overflow-y: auto;
            border: 1px solid #ddd;
            border-radius: 4px;
            background-color: #fafafa;
        }
        .country-table {
            width: 100%;
            border-collapse: collapse;
        }
        .country-table th, .country-table td {
            padding: 10px 12px;
            border: 1px solid #eee;
            text-align: left;
        }
        .country-table th {
            background-color: #007bff;
            color: white;
            position: sticky; 
            top: 0;
            z-index: 10;
        }
        .country-table td:first-child {
            font-weight: bold;
            color: #333;
        }
        .country-table tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .country-table tr:hover {
            background-color: #e0f7fa;
        }
        .no-results {
            text-align: center;
            padding: 20px;
            color: #d9534f;
            font-weight: bold;
        }
    </style>
</head>
<body>

    <div class="container">
        <h1>护照信息辅助工具</h1>

        <div class="format-group">
            <h2>📝 英文格式规范化 (首字母大写)</h2>
            <label for="rawInput">输入全大写的英文内容 (如护照上的姓名):</label>
            <div class="format-input-row">
                <input type="text" id="rawInput" placeholder="例如:ZHANG SANFENG" oninput="formatText()">
            </div>
            <label for="formattedOutput" style="margin-top: 15px;">规范化结果 (首字母大写):</label>
            <div id="formattedOutput"></div>
            <small style="color: #6c757d;">适用于护照姓名的标准格式,点击结果可选中复制。</small>
        </div>
        <label for="countryCodeInput">输入代码、英文名或中文名进行模糊搜索:</label>
        <input type="text" id="countryCodeInput" placeholder="例如:CHN, China, 中国, United" oninput="liveSearch()">

        <div id="countryListSection">
            <h2>🌍 国家代码对照表</h2>
            <div class="country-table-container">
                <table id="countryListTable" class="country-table">
                    <thead>
                        <tr>
                            <th>代码 (Code)</th>
                            <th>英文名称 (English Name)</th>
                            <th>中文名称 (Chinese Name)</th>
                        </tr>
                    </thead>
                    <tbody>
                        </tbody>
                </table>
            </div>
        </div>

    </div>

    <script>
        // 核心数据结构: 存储国家代码、英文名和中文名 
        const countryData = {
            "AFG": { english: "Afghanistan", chinese: "阿富汗" },
            "ALA": { english: "Åland Islands", chinese: "奥兰群岛" },
            "ALB": { english: "Albania", chinese: "阿尔巴尼亚" },
            "DZA": { english: "Algeria", chinese: "阿尔及利亚" },
            "ASM": { english: "American Samoa", chinese: "美属萨摩亚" },
            "AND": { english: "Andorra", chinese: "安道尔" },
            "AGO": { english: "Angola", chinese: "安哥拉" },
            "AIA": { english: "Anguilla", chinese: "安圭拉" },
            "ATA": { english: "Antarctica", chinese: "南极洲" },
            "ATG": { english: "Antigua and Barbuda", chinese: "安提瓜和巴布达" },
            "ARG": { english: "Argentina", chinese: "阿根廷" },
            "ARM": { english: "Armenia", chinese: "亚美尼亚" },
            "ABW": { english: "Aruba", chinese: "阿鲁巴" },
            "AUS": { english: "Australia", chinese: "澳大利亚" },
            "AUT": { english: "Austria", chinese: "奥地利" },
            "AZE": { english: "Azerbaijan", chinese: "阿塞拜疆" },
            "BHS": { english: "Bahamas (The)", chinese: "巴哈马" },
            "BHR": { english: "Bahrain", chinese: "巴林" },
            "BGD": { english: "Bangladesh", chinese: "孟加拉国" },
            "BRB": { english: "Barbados", chinese: "巴巴多斯" },
            "BLR": { english: "Belarus", chinese: "白俄罗斯" },
            "BEL": { english: "Belgium", chinese: "比利时" },
            "BLZ": { english: "Belize", chinese: "伯利兹" },
            "BEN": { english: "Benin", chinese: "贝宁" },
            "BMU": { english: "Bermuda", chinese: "百慕大" },
            "BTN": { english: "Bhutan", chinese: "不丹" },
            "BOL": { english: "Bolivia (Plurinational State of)", chinese: "玻利维亚" },
            "BES": { english: "Bonaire, Sint Eustatius and Saba", chinese: "博内尔、圣尤斯塔蒂乌斯和萨巴" },
            "BIH": { english: "Bosnia and Herzegovina", chinese: "波斯尼亚和赫塞哥维那" },
            "BWA": { english: "Botswana", chinese: "博茨瓦纳" },
            "BVT": { english: "Bouvet Island", chinese: "布韦岛" },
            "BRA": { english: "Brazil", chinese: "巴西" },
            "IOT": { english: "British Indian Ocean Territory (The)", chinese: "英属印度洋领地" },
            "VGB": { english: "British Virgin Islands", chinese: "英属维尔京群岛" },
            "BRN": { english: "Brunei Darussalam", chinese: "文莱达鲁萨兰国" },
            "BGR": { english: "Bulgaria", chinese: "保加利亚" },
            "BFA": { english: "Burkina Faso", chinese: "布基纳法索" },
            "BDI": { english: "Burundi", chinese: "布隆迪" },
            "CPV": { english: "Cabo Verde", chinese: "佛得角" },
            "KHM": { english: "Cambodia", chinese: "柬埔寨" },
            "CMR": { english: "Cameroon", chinese: "喀麦隆" },
            "CAN": { english: "Canada", chinese: "加拿大" },
            "CYM": { english: "Cayman Islands (The)", chinese: "开曼群岛" },
            "CAF": { english: "Central African Republic (The)", chinese: "中非共和国" },
            "TCD": { english: "Chad", chinese: "乍得" },
            "CHL": { english: "Chile", chinese: "智利" },
            "CHN": { english: "China", chinese: "中国" },
            "CXR": { english: "Christmas Island", chinese: "圣诞岛" },
            "CCK": { english: "Cocos (Keeling) Islands (The)", chinese: "科科斯(基林)群岛" },
            "COL": { english: "Colombia", chinese: "哥伦比亚" },
            "COM": { english: "Comoros (The)", chinese: "科摩罗" },
            "COD": { english: "Congo (The Democratic Republic of the)", chinese: "刚果(金)" },
            "COG": { english: "Congo (The)", chinese: "刚果(布)" },
            "COK": { english: "Cook Islands (The)", chinese: "库克群岛" },
            "CRI": { english: "Costa Rica", chinese: "哥斯达黎加" },
            "CIV": { english: "Côte d'Ivoire", chinese: "科特迪瓦" },
            "HRV": { english: "Croatia", chinese: "克罗地亚" },
            "CUB": { english: "Cuba", chinese: "古巴" },
            "CUW": { english: "Curaçao", chinese: "库拉索" },
            "CYP": { english: "Cyprus", chinese: "塞浦路斯" },
            "CZE": { english: "Czechia", chinese: "捷克" },
            "DNK": { english: "Denmark", chinese: "丹麦" },
            "DJI": { english: "Djibouti", chinese: "吉布提" },
            "DMA": { english: "Dominica", chinese: "多米尼克" },
            "DOM": { english: "Dominican Republic (The)", chinese: "多米尼加共和国" },
            "ECU": { english: "Ecuador", chinese: "厄瓜多尔" },
            "EGY": { english: "Egypt", chinese: "埃及" },
            "SLV": { english: "El Salvador", chinese: "萨尔瓦多" },
            "GNQ": { english: "Equatorial Guinea", chinese: "赤道几内亚" },
            "ERI": { english: "Eritrea", chinese: "厄立特里亚" },
            "EST": { english: "Estonia", chinese: "爱沙尼亚" },
            "SWZ": { english: "Eswatini", chinese: "斯威士兰" },
            "ETH": { english: "Ethiopia", chinese: "埃塞俄比亚" },
            "FLK": { english: "Falkland Islands (The) [Malvinas]", chinese: "福克兰群岛" },
            "FRO": { english: "Faroe Islands (The)", chinese: "法罗群岛" },
            "FJI": { english: "Fiji", chinese: "斐济" },
            "FIN": { english: "Finland", chinese: "芬兰" },
            "FRA": { english: "France", chinese: "法国" },
            "GUF": { english: "French Guiana", chinese: "法属圭亚那" },
            "PYF": { english: "French Polynesia", chinese: "法属波利尼西亚" },
            "ATF": { english: "French Southern Territories (The)", chinese: "法属南部领地" },
            "GAB": { english: "Gabon", chinese: "加蓬" },
            "GMB": { english: "Gambia (The)", chinese: "冈比亚" },
            "GEO": { english: "Georgia", chinese: "格鲁吉亚" },
            "DEU": { english: "Germany", chinese: "德国" },
            "GHA": { english: "Ghana", chinese: "加纳" },
            "GIB": { english: "Gibraltar", chinese: "直布罗陀" },
            "GRC": { english: "Greece", chinese: "希腊" },
            "GRL": { english: "Greenland", chinese: "格陵兰" },
            "GRD": { english: "Grenada", chinese: "格林纳达" },
            "GLP": { english: "Guadeloupe", chinese: "瓜德罗普" },
            "GUM": { english: "Guam", chinese: "关岛" },
            "GTM": { english: "Guatemala", chinese: "危地马拉" },
            "GGY": { english: "Guernsey", chinese: "根西岛" },
            "GIN": { english: "Guinea", chinese: "几内亚" },
            "GNB": { english: "Guinea-Bissau", chinese: "几内亚比绍" },
            "GUY": { english: "Guyana", chinese: "圭亚那" },
            "HTI": { english: "Haiti", chinese: "海地" },
            "HMD": { english: "Heard Island and McDonald Islands", chinese: "赫德岛和麦克唐纳群岛" },
            "VAT": { english: "Holy See (The)", chinese: "梵蒂冈" },
            "HND": { english: "Honduras", chinese: "洪都拉斯" },
            "HKG": { english: "Hong Kong", chinese: "中国香港" },
            "HUN": { english: "Hungary", chinese: "匈牙利" },
            "ISL": { english: "Iceland", chinese: "冰岛" },
            "IND": { english: "India", chinese: "印度" },
            "IDN": { english: "Indonesia", chinese: "印度尼西亚" },
            "IRN": { english: "Iran (Islamic Republic of)", chinese: "伊朗" },
            "IRQ": { english: "Iraq", chinese: "伊拉克" },
            "IRL": { english: "Ireland", chinese: "爱尔兰" },
            "IMN": { english: "Isle of Man", chinese: "马恩岛" },
            "ISR": { english: "Israel", chinese: "以色列" },
            "ITA": { english: "Italy", chinese: "意大利" },
            "JAM": { english: "Jamaica", chinese: "牙买加" },
            "JPN": { english: "Japan", chinese: "日本" },
            "JEY": { english: "Jersey", chinese: "泽西岛" },
            "JOR": { english: "Jordan", chinese: "约旦" },
            "KAZ": { english: "Kazakhstan", chinese: "哈萨克斯坦" },
            "KEN": { english: "Kenya", chinese: "肯尼亚" },
            "KIR": { english: "Kiribati", chinese: "基里巴斯" },
            "PRK": { english: "Korea (The Democratic People's Republic of)", chinese: "朝鲜" },
            "KOR": { english: "Korea (The Republic of)", chinese: "韩国" },
            "KWT": { english: "Kuwait", chinese: "科威特" },
            "KGZ": { english: "Kyrgyzstan", chinese: "吉尔吉斯斯坦" },
            "LAO": { english: "Lao People's Democratic Republic (The)", chinese: "老挝" },
            "LVA": { english: "Latvia", chinese: "拉脱维亚" },
            "LBN": { english: "Lebanon", chinese: "黎巴嫩" },
            "LSO": { english: "Lesotho", chinese: "莱索托" },
            "LBR": { english: "Liberia", chinese: "利比里亚" },
            "LBY": { english: "Libya", chinese: "利比亚" },
            "LIE": { english: "Liechtenstein", chinese: "列支敦士登" },
            "LTU": { english: "Lithuania", chinese: "立陶宛" },
            "LUX": { english: "Luxembourg", chinese: "卢森堡" },
            "MAC": { english: "Macao", chinese: "中国澳门" },
            "MDG": { english: "Madagascar", chinese: "马达加斯加" },
            "MWI": { english: "Malawi", chinese: "马拉维" },
            "MYS": { english: "Malaysia", chinese: "马来西亚" },
            "MDV": { english: "Maldives", chinese: "马尔代夫" },
            "MLI": { english: "Mali", chinese: "马里" },
            "MLT": { english: "Malta", chinese: "马耳他" },
            "MHL": { english: "Marshall Islands (The)", chinese: "马绍尔群岛" },
            "MTQ": { english: "Martinique", chinese: "马提尼克" },
            "MRT": { english: "Mauritania", chinese: "毛里塔尼亚" },
            "MUS": { english: "Mauritius", chinese: "毛里求斯" },
            "MYT": { english: "Mayotte", chinese: "马约特" },
            "MEX": { english: "Mexico", chinese: "墨西哥" },
            "FSM": { english: "Micronesia (Federated States of)", chinese: "密克罗尼西亚" },
            "MDA": { english: "Moldova (The Republic of)", chinese: "摩尔多瓦" },
            "MCO": { english: "Monaco", chinese: "摩纳哥" },
            "MNG": { english: "Mongolia", chinese: "蒙古" },
            "MNE": { english: "Montenegro", chinese: "黑山" },
            "MSR": { english: "Montserrat", chinese: "蒙特塞拉特" },
            "MAR": { english: "Morocco", chinese: "摩洛哥" },
            "MOZ": { english: "Mozambique", chinese: "莫桑比克" },
            "MMR": { english: "Myanmar", chinese: "缅甸" },
            "NAM": { english: "Namibia", chinese: "纳米比亚" },
            "NRU": { english: "Nauru", chinese: "瑙鲁" },
            "NPL": { english: "Nepal", chinese: "尼泊尔" },
            "NLD": { english: "Netherlands (The)", chinese: "荷兰" },
            "NCL": { english: "New Caledonia", chinese: "新喀里多尼亚" },
            "NZL": { english: "New Zealand", chinese: "新西兰" },
            "NIC": { english: "Nicaragua", chinese: "尼加拉瓜" },
            "NER": { english: "Niger (The)", chinese: "尼日尔" },
            "NGA": { english: "Nigeria", chinese: "尼日利亚" },
            "NIU": { english: "Niue", chinese: "纽埃" },
            "NFK": { english: "Norfolk Island", chinese: "诺福克岛" },
            "MNP": { english: "Northern Mariana Islands (The)", chinese: "北马里亚纳群岛" },
            "NOR": { english: "Norway", chinese: "挪威" },
            "OMN": { english: "Oman", chinese: "阿曼" },
            "PAK": { english: "Pakistan", chinese: "巴基斯坦" },
            "PLW": { english: "Palau", chinese: "帕劳" },
            "PSE": { english: "Palestine, State of", chinese: "巴勒斯坦" },
            "PAN": { english: "Panama", chinese: "巴拿马" },
            "PNG": { english: "Papua New Guinea", chinese: "巴布亚新几内亚" },
            "PRY": { english: "Paraguay", chinese: "巴拉圭" },
            "PER": { english: "Peru", chinese: "秘鲁" },
            "PHL": { english: "Philippines (The)", chinese: "菲律宾" },
            "PCN": { english: "Pitcairn", chinese: "皮特凯恩" },
            "POL": { english: "Poland", chinese: "波兰" },
            "PRT": { english: "Portugal", chinese: "葡萄牙" },
            "PRI": { english: "Puerto Rico", chinese: "波多黎各" },
            "QAT": { english: "Qatar", chinese: "卡塔尔" },
            "MKD": { english: "Republic of North Macedonia", chinese: "北马其顿" },
            "ROU": { english: "Romania", chinese: "罗马尼亚" },
            "RUS": { english: "Russian Federation (The)", chinese: "俄罗斯" },
            "RWA": { english: "Rwanda", chinese: "卢旺达" },
            "REU": { english: "Réunion", chinese: "留尼汪" },
            "BLM": { english: "Saint Barthélemy", chinese: "圣巴泰勒米" },
            "SHN": { english: "Saint Helena, Ascension and Tristan da Cunha", chinese: "圣赫勒拿、阿森松和特里斯坦-达库尼亚" },
            "KNA": { english: "Saint Kitts and Nevis", chinese: "圣基茨和尼维斯" },
            "LCA": { english: "Saint Lucia", chinese: "圣卢西亚" },
            "MAF": { english: "Saint Martin (French part)", chinese: "法属圣马丁" },
            "SPM": { english: "Saint Pierre and Miquelon", chinese: "圣皮埃尔和密克隆" },
            "VCT": { english: "Saint Vincent and the Grenadines", chinese: "圣文森特和格林纳丁斯" },
            "WSM": { english: "Samoa", chinese: "萨摩亚" },
            "SMR": { english: "San Marino", chinese: "圣马力诺" },
            "STP": { english: "Sao Tome and Principe", chinese: "圣多美和普林西比" },
            "SAU": { english: "Saudi Arabia", chinese: "沙特阿拉伯" },
            "SEN": { english: "Senegal", chinese: "塞内加尔" },
            "SRB": { english: "Serbia", chinese: "塞尔维亚" },
            "SYC": { english: "Seychelles", chinese: "塞舌尔" },
            "SLE": { english: "Sierra Leone", chinese: "塞拉利昂" },
            "SGP": { english: "Singapore", chinese: "新加坡" },
            "SXM": { english: "Sint Maarten (Dutch part)", chinese: "荷属圣马丁" },
            "SVK": { english: "Slovakia", chinese: "斯洛伐克" },
            "SVN": { english: "Slovenia", chinese: "斯洛文尼亚" },
            "SLB": { english: "Solomon Islands", chinese: "所罗门群岛" },
            "SOM": { english: "Somalia", chinese: "索马里" },
            "ZAF": { english: "South Africa", chinese: "南非" },
            "SGS": { english: "South Georgia and the South Sandwich Islands", chinese: "南乔治亚岛和南桑威奇群岛" },
            "SSD": { english: "South Sudan", chinese: "南苏丹" },
            "ESP": { english: "Spain", chinese: "西班牙" },
            "LKA": { english: "Sri Lanka", chinese: "斯里兰卡" },
            "SDN": { english: "Sudan (The)", chinese: "苏丹" },
            "SUR": { english: "Suriname", chinese: "苏里南" },
            "SJM": { english: "Svalbard and Jan Mayen", chinese: "斯瓦尔巴和扬马延" },
            "SWE": { english: "Sweden", chinese: "瑞典" },
            "CHE": { english: "Switzerland", chinese: "瑞士" },
            "SYR": { english: "Syrian Arab Republic", chinese: "叙利亚" },
            "TWN": { english: "Taiwan (Province of China)", chinese: "中国台湾" },
            "TJK": { english: "Tajikistan", chinese: "塔吉克斯坦" },
            "TZA": { english: "Tanzania, United Republic of", chinese: "坦桑尼亚" },
            "THA": { english: "Thailand", chinese: "泰国" },
            "TLS": { english: "Timor-Leste", chinese: "东帝汶" },
            "TGO": { english: "Togo", chinese: "多哥" },
            "TKL": { english: "Tokelau", chinese: "托克劳" },
            "TON": { english: "Tonga", chinese: "汤加" },
            "TTO": { english: "Trinidad and Tobago", chinese: "特立尼达和多巴哥" },
            "TUN": { english: "Tunisia", chinese: "突尼斯" },
            "TUR": { english: "Turkey", chinese: "土耳其" },
            "TKM": { english: "Turkmenistan", chinese: "土库曼斯坦" },
            "TCA": { english: "Turks and Caicos Islands (The)", chinese: "特克斯和凯科斯群岛" },
            "TUV": { english: "Tuvalu", chinese: "图瓦卢" },
            "UGA": { english: "Uganda", chinese: "乌干达" },
            "UKR": { english: "Ukraine", chinese: "乌克兰" },
            "ARE": { english: "United Arab Emirates (The)", chinese: "阿拉伯联合酋长国" },
            "GBR": { english: "United Kingdom of Great Britain and Northern Ireland (The)", chinese: "英国" },
            "UMI": { english: "United States Minor Outlying Islands (The)", chinese: "美国本土外小岛屿" },
            "USA": { english: "United States of America (The)", chinese: "美国" },
            "URY": { english: "Uruguay", chinese: "乌拉圭" },
            "UZB": { english: "Uzbekistan", chinese: "乌兹别克斯坦" },
            "VUT": { english: "Vanuatu", chinese: "瓦努阿图" },
            "VEN": { english: "Venezuela (Bolivarian Republic of)", chinese: "委内瑞拉" },
            "VNM": { english: "Viet Nam", chinese: "越南" },
            "VIR": { english: "Virgin Islands (U.S.)", chinese: "美属维尔京群岛" },
            "WLF": { english: "Wallis and Futuna", chinese: "瓦利斯和富图纳" },
            "ESH": { english: "Western Sahara", chinese: "西撒哈拉" },
            "YEM": { english: "Yemen", chinese: "也门" },
            "ZMB": { english: "Zambia", chinese: "赞比亚" },
            "ZWE": { english: "Zimbabwe", chinese: "津巴布韦" }
        };

        // --- 英文格式化函数 ---
        
        /**
         * 将输入的英文内容转换为首字母大写格式 (Title Case)
         * 例如: "ZHANG SANFENG" -> "Zhang Sanfeng"
         */
        function formatText() {
            const rawInput = document.getElementById('rawInput').value.trim();
            const outputDiv = document.getElementById('formattedOutput');

            if (rawInput === "") {
                outputDiv.textContent = "";
                return;
            }

            // 1. 将整个字符串转换为小写
            const lowerCaseText = rawInput.toLowerCase();

            // 2. 使用正则表达式查找每个单词(由空格、连字符或撇号分隔)并进行首字母大写
            const formattedText = lowerCaseText.replace(/(^|\s|-|\')\S/g, function(match) {
                // 将匹配到的第一个字符转换为大写
                return match.toUpperCase();
            });

            // 3. 将结果显示在旁边的div中
            outputDiv.textContent = formattedText;
        }


        // --- 国家代码模糊搜索函数 ---

        // 在 searchableCountries 的定义中加入城市名和别名,例如阿联酋 (ARE)
        const searchableCountries = Object.keys(countryData).map(code => {
            let extraTerms = "";
            if (code === "ARE") {
                // 添加迪拜、阿布扎比等城市名方便搜索
                extraTerms = "Dubai 迪拜 Abu Dhabi 阿布扎比"; 
            }
            // 可以为其他国家/地区添加别名,例如:
            // if (code === "GBR") { extraTerms = "England UK 英国"; }

            return {
                code: code,
                english: countryData[code].english,
                chinese: countryData[code].chinese,
                searchTerms: `${code} ${countryData[code].english} ${countryData[code].chinese} ${extraTerms}`.toLowerCase()
            };
        });
        
        const tableBody = document.querySelector('#countryListTable tbody');

        /**
         * 动态渲染匹配到的国家列表
         */
        function renderCountryList(countriesToRender) {
            tableBody.innerHTML = ''; 
            const fragment = document.createDocumentFragment();

            if (countriesToRender.length === 0) {
                const row = document.createElement('tr');
                row.classList.add('no-results');
                const td = document.createElement('td');
                td.setAttribute('colspan', '3');
                td.textContent = "未找到匹配项。请尝试其他代码、英文名或中文名。";
                row.appendChild(td);
                fragment.appendChild(row);
            } else {
                countriesToRender.forEach(country => {
                    const row = document.createElement('tr');
                    
                    const tdCode = document.createElement('td');
                    tdCode.textContent = country.code;
                    row.appendChild(tdCode);

                    const tdEnglish = document.createElement('td');
                    tdEnglish.textContent = country.english;
                    row.appendChild(tdEnglish);

                    const tdChinese = document.createElement('td');
                    tdChinese.textContent = country.chinese;
                    row.appendChild(tdChinese);

                    fragment.appendChild(row);
                });
            }
            
            tableBody.appendChild(fragment);
        }

        /**
         * 实时搜索国家代码/名称
         */
        function liveSearch() {
            const inputElement = document.getElementById('countryCodeInput');
            const searchText = inputElement.value.toLowerCase().trim(); 

            let filteredCountries;

            if (searchText.length === 0) {
                filteredCountries = searchableCountries.sort((a, b) => a.code.localeCompare(b.code));
                document.querySelector('#countryListSection h2').textContent = "🌍 所有国家代码对照表";
            } else {
                filteredCountries = searchableCountries.filter(country => {
                    return country.searchTerms.includes(searchText);
                }).sort((a, b) => a.code.localeCompare(b.code));
                
                document.querySelector('#countryListSection h2').textContent = `🔎 搜索结果 (匹配 ${filteredCountries.length} 条)`;
            }

            renderCountryList(filteredCountries);
        }

        // 页面加载完成后,初始化两个功能
        document.addEventListener('DOMContentLoaded', () => {
            liveSearch(); // 初始显示国家列表
            formatText(); // 初始检查格式化输入框
        });
    </script>

</body>
</html>

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注