找回密码
 立即注册
搜索
查看: 257|回复: 0

[基础语法] 利用pinyin-pro带声调,给编辑器输出的问题备注拼音

[复制链接]

581

主题

110

回帖

4066

积分

管理员

积分
4066

众神之神

发表于 2024-10-20 19:14:11 | 显示全部楼层 |阅读模式
  1. <div id="content-container">
  2.                     {$new['content']}
  3.                 </div>
  4.                
  5.                 <script src="https://cdn.bootcdn.net/ajax/libs/pinyin-pro/3.21.1/index.js"></script>
  6.                 <script>
  7.                     document.addEventListener('DOMContentLoaded', function() {
  8.                         const container = document.getElementById('content-container');
  9.                         const content = container.innerHTML;
  10.                         
  11.                         const processedContent = content.replace(/[\u4e00-\u9fa5]+/g, function(match) {
  12.                             const pinyinArray = pinyinPro.pinyin(match, { toneType: 'symbol', type: 'array' });
  13.                             const pinyinHtml = pinyinArray.map((pinyin, index) =>
  14.                                 `<ruby>${match[index]}<rt>${pinyin}</rt></ruby>`
  15.                             ).join('');
  16.                             return pinyinHtml;
  17.                         });
  18.                         
  19.                         container.innerHTML = processedContent;
  20.                     });
  21.                 </script>
复制代码



JS如果不用cdn保存在本地加载,速度也许会快一点;
这一版是拼音带有声调的;
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|十三博客 ( 鲁ICP备2023000528号 )

GMT+8, 2026-6-1 16:14 , Processed in 0.175336 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表