全角转半角 · 文件名小写 · 句子格式化 · 特殊单词保留 · 输入法友好
<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>
// 基础用法
$('#myInput').smartFormat();
// 带配置
$('#myInput').smartFormat({
debug: true,
specialWords: { 'ios': 'iOS' }
});
// 手动格式化
$('#input').smartFormat('format');
// 获取格式化后的值
var val = $('#input').smartFormat('getValue');
// 更新配置
$('#input').smartFormat('updateOptions', { debug: true });
// 销毁插件
$('#input').smartFormat('destroy');
| 方法 | 语法 | 返回值 | 说明 |
|---|---|---|---|
初始化 | $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对象 | 销毁插件 |
| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
fullWidthToHalf | boolean | true | 全角转半角 |
fileNameLowercase | boolean | true | 文件名小写 |
sentenceFormat | boolean | true | 句子格式化 |
preserveSpecialWords | boolean | true | 保留特殊单词 |
formatOnCompositionEnd | boolean | true | 输入法确认后格式化 |
autoFormat | boolean | true | 输入时自动格式化 |
formatOnBlur | boolean | true | 失去焦点时格式化 |
| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
showLivePreview | boolean | false | 显示实时预览 |
previewElement | string/object | null | 预览元素选择器 |
specialWords | object | 内置映射 | 特殊单词映射表 |
fileExtensions | array | 内置列表 | 文件扩展名列表 |
debug | boolean | false | 调试模式 |
onFormat | function | null | 格式化回调 |
onError | function | null | 错误回调 |
$('#title').smartFormat();
适用于标题、名称等单行输入
$('.formatted').smartFormat({
debug: false
});
适用于表单多个输入框
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 选项扩展
💡 可通过 fileExtensions 选项扩展