最近项目需要一个功能,实现汉字转拼音功能,具体比如说输入一个“你好”,同时带出对应拼音“NiHao”,在此做一下记录

1、首先引入两个文件

    <script src="jquery.min.js"></script>
<script src="Convert_Pinyin.js"></script>

2、html设计

 <body>
<div>
输入名称:<input type="text" id="chinaName" onBlur="ConvertName()" /> <br/>
全写拼音:<input type="text" id="fullName" /> <br/>
简写拼音:<input type="text" id="easyName" /> <br/>
</div>
</body>

3、js方法

<script>
var ConvertName = function(){
var chinaName = $('#chinaName').val();
//获取全写拼音(调用js中方法)
var fullName = pinyin.getFullChars(chinaName);
//获取简写拼音(调用js中方法)
var easyName = pinyin.getCamelChars(chinaName);
//给两个文本框赋值
$('#fullName').val(fullName);
$('#easyName').val(easyName);
}
</script>

4、实现效果

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAR0AAABgCAIAAACvwzGHAAAI10lEQVR4nO2dS5LcKhBFtUOWxTp6pMkLb4OlaOod1BuITwLJRyILpPY90WG3VSqEVBwSKMDbXwCANNvqDADwC9n+/PcHP/jBj+zP9gEASAOvAJAHXgEgD7wCQB54BYA88AoAeeAVAPLAKwDkgVcAyAOvAJAHXll+APj5kSpOr/fq2NWm9mM4HcFnCl7KPK+OXW3aZIeN3jaR0hwn2Zfisattc9eX0gpegXlesVrZg0ZzxoVTrpT2Y1ctq5xN20Yv229jC3gFZnhl9BZjS7PRriAXYtnn8/kch9nVVvYuPjkYwxFrE180yyT7ni7gFZgVr4JD/IF6WDK6w6xTKvY0NohRr9hgdbdhmD3TY1fkQn1VRA98YunRegQPn4No1v51JnkVW3TsalMqCS1K60oxbph1OqX2Iy8dmW6FoBaCqP0106qjifn5fAa9KsscRVRtPp9jV8UzzxfsW7SJbzq8K9QtbLyW7ff+S8zx6vygzz9rUaD42vmpl95GpLPNxlAFl0JYrKrZXdL+aG7yd7wqNUA56T80Ea5+cOfZK4arBQeJjf5xc+3worWggyle2fozrUbTKrHcyzJ62/Re7oQlhBJXKRkuE8kpXU3OOn1eXS63IRW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pWP92V8y7DV1zqIgv2rNs7HQRx26WRfrw4HKS85lL4y6TOcyMvFT4xLS/RlbSGS8hL/bvedIkOQf+N9q9Q0l/Hu+WSO+NVl8KLareXmre13MC60m4puES3l1zxk1d1QTYLP4T2kjGm+n/G9FJ8xGbBn3XiubES+UHeruMZt2G0jhf8LwJ2J3/R4Rk6VKLv+pX+tJJLft9j9El++Rayn1AoJavnXQbMueDhvTFEenit1wnc7qldGbjEw98dj5d04a+XTtiASzz8XZeietzfTUoGLnHxl10CB3CJh5mLCIBluBlFcAkAHuASADzAJWNQxwO/3w91PBbu30ewOpNdot44ML6FyMa25sOgKycipvUOA5fAVJfyYURyJ8cW2eEsfZSnVUSaHj9gTS/wEPNciobSpwOfiaFvfcNPWlOUsKYXeItZLp1jH3dlhxgLpeOQTUN6x5peYGnmtpe0VsHQ6K5Qb1X1yvOXzOfX9OqeT1uBuJB81jl5s8Bk5vfjVccxkzNl+4Or36VRhlzKsh6jS3nxkjVXij+CyUx0KZmoRz5Ms1i6NmNUq6XW9OJ2KW4TEv2j681NXZnHXEq722iXrkS73Tf4/MfX9OJ1iZjCC2/e5GMuNVh8TS+6mEMC5ElobG0iwMfX6nh1Fl/Tiy0vUa+7C9PZ0WJ6jK/kpT+xpheTS/SHyK2o+D3IV1zqIgv2rNv722t6sbhU/EhpvBZceoiV6nh0KC60plfbpebgj+pSV2lh8w56MJFX8hK1T/EQdKi736y0plfTpS6VSs8lf8TyowFM5BGX6N/lcbAwLGOIqq+1wdfB/CUeMB4PwCUe4BKASzzAJQCXeJi5iABYhptRBJcA4AEuAcADXAKAB7gEAA9wCQAe4BIAPMAlAHiASwDwAJcA4AEuAcADXAKAB7gEAA9wCQAe4BIAPPwDqq0FD+t5XhMAAAAASUVORK5CYII=" alt="" />

5、整体demo

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>如何实现汉字转拼音功能</title>
</head>
<script src="jquery.min.js"></script>
<script src="Convert_Pinyin.js"></script>
<script>
var ConvertName = function(){
var chinaName = $('#chinaName').val();
//获取全写拼音(调用js中方法)
var fullName = pinyin.getFullChars(chinaName);
//获取简写拼音(调用js中方法)
var easyName = pinyin.getCamelChars(chinaName);
//给两个文本框赋值
$('#fullName').val(fullName);
$('#easyName').val(easyName);
} </script>
<body>
<div>
输入名称:<input type="text" id="chinaName" onBlur="ConvertName()" /> <br/>
全写拼音:<input type="text" id="fullName" /> <br/>
简写拼音:<input type="text" id="easyName" /> <br/>
</div>
</body>
</html>

6、汉字转拼音js下载路径:

链接:https://pan.baidu.com/s/1NZ4noIgHv2HSzZW6yBRTxA    密码:2kv1

js如何简单实现汉字转成拼音的功能的更多相关文章

  1. Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音

    转载:http://blog.csdn.net/xiaanming/article/details/12684155 转载请注明出处:http://blog.csdn.net/xiaanming/ar ...

  2. 基于jQuery实现汉字转换成拼音代码

    基于jQuery实现汉字转换成拼音代码.这是一款基于jQuery.Hz2Py.js插件实现的汉字转拼音特效.效果图如下: 在线预览   源码下载 实现的代码. html代码: <br /> ...

  3. SQL 汉字转换成拼音首字母 首字母查

    -- ============================================= -- 功能:汉字转换成拼音首字母 首字母查 -- ========================== ...

  4. C# 将汉字转化成拼音

    本文来自http://www.cnblogs.com/yazdao/archive/2011/06/04/2072488.html 首先下载Visual Studio International Pa ...

  5. python把汉字转换成拼音实现程序

    python把汉字转换成拼音实现程序 文章一个简洁干的汉字转拼音 程序,复制下载就可以直接使用,有需要的同学可以参考一下下. #coding:utf-8 #基于python2.6 table = 'a ...

  6. C++ 将汉字转换成拼音全拼

    #include <string> using std::string; //======================================================= ...

  7. C++ 将汉字转换成拼音全拼【转载】

    转载自https://www.cnblogs.com/mzhrd/p/4758105.html #include <string> using std::string; //======= ...

  8. 使用 js 实现一个中文自动转换成拼音的工具库

    使用 js 实现一个中文自动转换成拼音的工具库 中文 => zhong-wen 应用场景 SEO 友好, URL 自动转换 blogs 发布文章,自动化部署,自动生成 url 的 path (时 ...

  9. Python汉字转换成拼音

    最近在使用Python做项目时,需要将汉字转化成对应的拼音. 网上的一些包大多是python2.x的,使用下面这个包,支持python3.6 xpinyin 0.5.5 >>> fr ...

随机推荐

  1. HP惠普笔记本安装VirtualBox后 不能选择64bit的系统

    之前在台式机上安装VirtualBox,一切OK,能够安装64位的任何版本iso包今天在hp笔记本上安装,安装VirtualBox完毕后,只能选择32位的iso版本. 而我目前只有一个linux64b ...

  2. Oracle存储过程的异常处理

    1.为了提高存储过程的健壮性,避免运行错误,当建立存储过程时应包含异常处理部分. 2.异常(EXCEPTION)是一种PL/SQL标识符,包括预定义异常.非预定义异常和自定义异常: 3.预定义异常是指 ...

  3. flutter安装与配置 v1.2.1版本

    1---- 上面是下载地址https://flutter.dev/docs/development/tools/sdk/archive#windows 2---- 下载后,解压安装到C盘 3--- 测 ...

  4. variable_scope

    1. with tf.variable_scope("a"): b=tf.get_variable(name="g",initializer=12) print ...

  5. 第 3 章 镜像 - 017 - RUN vs CMD vs ENTRYPOINT

    RUN.CMD 和 ENTRYPOINT 这三个 Dockerfile 指令看上去很类似,很容易混淆. 简单的说: RUN 执行命令并创建新的镜像层,RUN 经常用于安装软件包. CMD 设置容器启动 ...

  6. 绕过cookies进行登录并封装请求方法

    之前写了一篇使用session跨请求保持会话的帖子,这次在它的基础上对请求方法简单封装一下,可以达到复用的效果 1.先定义登录方法 在登录方法中利用session跨请求保持会话,并返回session, ...

  7. legend2---开发日志7(vue的使用场景有哪些,或者说使用的优缺点)

    legend2---开发日志7(vue的使用场景有哪些,或者说使用的优缺点) 一.总结 一句话总结:任何页面都可以使用vue,尤其是有交互的或者用ajax的,但是vue插件及其它各种插件加载需要时间, ...

  8. 关于 Oracle DB CONSTRAINT约束的一些SQL ORA-02292: integrity constraint violated

    ALTER TABLE table_name DISABLE CONSTRAINT constraint_name; select * from all_constraints where owner ...

  9. English trip V1 - B 21. On a busy day 忙碌的一天 Teacher:Taylor Key: at on in

    In this lesson you will learn to tell the time.  说时间 课上内容(Lesson) at       time; at 7:30; at midday; ...

  10. android--------自定义控件 之 方法篇

    前面简单的讲述了Android中自定义控件的理论和流程图,今天通过代码来详细的讲解一下其中的方法 首先先创建一个类 CircularView 继承于 View,之后实现构造方法(初始化步骤) publ ...