index.html 4.2 KB

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