forked from sxei/pinyinjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
122 lines (117 loc) · 4.23 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8"/>
<title>汉字拼音互转示例</title>
<link rel="stylesheet" type="text/css" href="simple-input-method/simple-input-method.css">
<style type="text/css">
body{font-family: 'Microsoft Yahei'; font-size: 16px;}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
input[type="text"] {
height: 34px;
padding: 6px 12px;
font-size: 14px;
line-height: 1.42857143;
color: #555;
background-color: #fff;
background-image: none;
border: 1px solid #ccc;
border-radius: 4px;
box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
-webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
}
input[type="text"]:focus {
border-color: #66afe9;
outline: 0;
-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6);
box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6);
}
h2 > span {
color: #B10000;
font-size: 0.8em;
}
#test {margin-top: 10px;}
.loading-tip{color: #00960A;margin-bottom: 10px;}
</style>
</head>
<body>
<a href="http://blog.liuxianan.com/pinyinjs.html" target="_blank">返回原文</a>
<a href="http://github.com/liuxianan/pinyinjs/" target="_blank">github地址</a>
<h2>汉字转拼音:</h2>
<div>
<span>输出类型:</span>
<label><input type="radio" name="pinyin_type" value="0" checked/>带声调拼音</label>
<label><input type="radio" name="pinyin_type" value="1"/>不带声调拼音</label>
<label><input type="radio" name="pinyin_type" value="2"/>拼音首字母</label>
</div>
<div>
<span>多音字:</span>
<label><input type="checkbox" name="polyphone"/>简单支持多音字</label>
<span style="color: #9E9E9E;">(支持多音字仅仅是将所有可能的组合列举出来,要做到准确识别多音字还需非常完善的词库)</span>
<p>带词库的“准确”识别多音字示例请<a href="polyphone.html">单独点击这里查看</a></p>
</div>
<input type="text" id="test" value="大中国" placeholder="请随便输入一些中文"/>
<h3>转换结果:</h3>
<div id="result"></div>
<h2>JS实现简单的拼音输入法<span>(请将系统输入法设置为英文):</span></h2>
<div class="loading-tip">正在加载JS,请等待加载完毕再尝试。</div>
<input type="text" class="test-input-method" placeholder="请在这里打字试试">
<input type="text" class="test-input-method" placeholder="请在这里打字试试">
<input type="text" class="test-input-method" placeholder="请在这里打字试试">
<script type="text/javascript" src="dict/pinyin_dict_notone.js"></script>
<script type="text/javascript" src="dict/pinyin_dict_withtone.js"></script>
<script type="text/javascript" src="pinyinUtil.js"></script>
<script type="text/javascript" src="simple-input-method/simple-input-method.js"></script>
<script type="text/javascript">
function getPinyin()
{
var value = document.getElementById('test').value;
var type = document.querySelector('[name="pinyin_type"]:checked').value;
var polyphone = document.querySelector('[name="polyphone"]').checked;
var result = '';
if(value)
{
switch(type)
{
case '0': result = pinyinUtil.getPinyin(value, ' ', true, polyphone); break;
case '1': result = pinyinUtil.getPinyin(value, ' ', false, polyphone); break;
case '2': result = pinyinUtil.getFirstLetter(value, polyphone); break;
default: break;
}
}
var html = result;
if(result instanceof Array)
{
html = '<ol>';
result.forEach(function(val)
{
html += '<li>'+val+'</li>';
});
html += '</ol>';
}
document.getElementById('result').innerHTML = html;
}
document.getElementById('test').addEventListener('input', getPinyin);
document.getElementsByName('polyphone')[0].addEventListener('change', function(e)
{
getPinyin();
});
document.addEventListener('change', function(e)
{
if(e.target.name === 'pinyin_type')
{
getPinyin();
}
});
getPinyin();
// 初始化简单的拼音输入法
SimpleInputMethod.init('.test-input-method');
document.querySelector('.loading-tip').innerHTML = 'JS加载完毕,现在你可以开始打字了!';
</script>
</body>
</html>