Regex和Markdown为文本字段中 字符着色 jQuery Colorfy

  • 源码大小:19.11KB
  • 所需积分:1积分
  • 源码编号:19JP-3089
  • 浏览次数:809次
  • 最后更新:2023年05月04日
  • 所属栏目:表单
本站默认解压密码:19jp.com 或 19jp_com

简介

Colorfy是一个小型jQuery插件,能够根据特定的Regex模式或Markdown语法自定义文本字段中字符的颜色。

可用于突出显示用户输入,以提高输入字段或文本区域元素的可读性。

参见:

  • Colorfy#标签和@可编辑内容中的提及-jQuery自动标签

如何使用它:

1.在页面上包含jQuery库和Colorfy插件。

  1. <!-- jQuery is required -->
  2. <script src="/path/to/cdn/jquery.slim.min.js"></script>
  3.  
  4. <!-- Colorfy Text Fields Using Regex -->
  5. <script src="/path/to/jquery.colorfy.js"></script>
  6.  
  7. <!-- Colorfy Text Fields Using Markdown -->
  8. <script src="/path/to/jquery.colorfy.markdown.js"></script>

2.使用Regex为输入字段着色。

  1. <input id="input" class="input" value="price $9.99"></input>
  1. $.fn.colorfy.money = {
  2. "currency": /\$/m,
  3. "number": /[0-9.]+/m
  4. };
  5. $('#input').colorfy("money");
  1. /* apply custom CSS styles */
  2. .money .currency {
  3. color: red;
  4. }
  5.  
  6. .money .number {
  7. color: green;
  8. }

3.使用Markdown为文本区域上色。

  1. <textarea id="area" class="area">
  2. Please try for your self
  3. # This is title
  4. > This is inline block
  5. Some text should be **strong**, some text should be _italic_
  6. Some text should be ~~stroke through~~
  7. - list
  8. - list, too
  9. 1. ordered list
  10. 2. ordered list, too
  11. You can customize the style through a very simple `css` like this
  12. ``` css
  13. .markdown .orderedlist {
  14. color: maroon;
  15. }
  16. .markdown .unorderedlist {
  17. color: silver;
  18. }
  19. ```
  20. </textarea>
  1. $('#area').colorfy("markdown");
  1. .markdown .title {
  2. color: red;
  3. }
  4.  
  5. .markdown .block {
  6. color: green;
  7. }
  8.  
  9. .markdown .strong {
  10. color: blue;
  11. }
  12.  
  13. /* more styles here */

预览截图