✨ jQuery 插件 | 智能输入格式化

SmartFormat

全角转半角 · 文件名小写 · 句子格式化 · 特殊单词保留 · 输入法友好

v1.0.2 | 支持中文/日文/韩文输入法

🎮 在线演示

📋 实时格式化结果将显示在这里

✨ 核心功能

🔤 全角字符转半角
📄 文件名自动小写
📝 首字母大写 + 单词首字母大写
🔠 特殊单词保留格式
🌐 中文/日文/韩文输入法支持
🎯 光标位置自动保持
⚙️ 可配置扩展名和特殊单词
🐛 调试模式支持

🚀 快速开始

1. 引入依赖

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://alsonzhu123.github.io/SmartFormat/smart-format1.0.2.js"></script>

2. 初始化插件

// 基础用法
$('#myInput').smartFormat();

// 带配置
$('#myInput').smartFormat({
    debug: true,
    specialWords: { 'ios': 'iOS' }
});

3. 方法调用

// 手动格式化
$('#input').smartFormat('format');

// 获取格式化后的值
var val = $('#input').smartFormat('getValue');

// 更新配置
$('#input').smartFormat('updateOptions', { debug: true });

// 销毁插件
$('#input').smartFormat('destroy');

📖 API 方法参考

方法语法返回值说明
初始化$el.smartFormat(options)jQuery对象初始化插件
format$el.smartFormat('format')String手动触发格式化
getValue$el.smartFormat('getValue')String获取格式化后的值
updateOptions$el.smartFormat('updateOptions', opts)jQuery对象动态更新配置
destroy$el.smartFormat('destroy')jQuery对象销毁插件

⚙️ 配置选项 (1/2)

选项类型默认值说明
fullWidthToHalfbooleantrue全角转半角
fileNameLowercasebooleantrue文件名小写
sentenceFormatbooleantrue句子格式化
preserveSpecialWordsbooleantrue保留特殊单词
formatOnCompositionEndbooleantrue输入法确认后格式化
autoFormatbooleantrue输入时自动格式化
formatOnBlurbooleantrue失去焦点时格式化

⚙️ 配置选项 (2/2)

选项类型默认值说明
showLivePreviewbooleanfalse显示实时预览
previewElementstring/objectnull预览元素选择器
specialWordsobject内置映射特殊单词映射表
fileExtensionsarray内置列表文件扩展名列表
debugbooleanfalse调试模式
onFormatfunctionnull格式化回调
onErrorfunctionnull错误回调

📝 场景1:简单输入框

$('#title').smartFormat();

适用于标题、名称等单行输入

📋 场景2:批量初始化

$('.formatted').smartFormat({
    debug: false
});

适用于表单多个输入框

➕ 场景3:动态添加元素

var $new = $('<input>');
$new.smartFormat();
$('#container').append($new);

适用于动态添加的表单项

🔠 内置特殊单词

{
    'iphone': 'iPhone',
    'ipad': 'iPad',
    'ios': 'iOS',
    'macos': 'macOS',
    'javascript': 'JavaScript',
    'jquery': 'jQuery',
    'vue.js': 'Vue.js',
    'react.js': 'React.js',
    'angular.js': 'Angular.js',
    'typescript': 'TypeScript'
}

💡 可通过 specialWords 选项扩展

📁 内置文件扩展名

ipaapkexedmg txtmdjsjson htmlcsspyjava pdfdocxlszip pngjpgmp3mp4 shbatsqllog

💡 可通过 fileExtensions 选项扩展

⚠️ 注意事项

📦 需要 jQuery 1.7+
🌐 支持中文、日文、韩文等 IME 输入法
🎯 格式化后自动保持光标位置
适合 < 5000 字符的文本
🔧 特殊单词不区分大小写匹配