在两个$$符号之间书写时识别 [英] Recognize when writing in between two $$ signs

查看:125
本文介绍了在两个$$符号之间书写时识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本字段,正在其中编写一些文本.

I have a textfield in which I'm writing some text.

为简单起见,我希望alert('Boom!')每次在两个$$之间开始写入时都跳出来.

For the sake of simplicity, I want that alert('Boom!') jumps out every time I start writing in between two $$.

例如,我有一个空白的文本字段并开始键入(光标为"|"符号)

For example I have a blank textfield and start typing (cursor is "|" sign)

Today is a really nice day|

什么也没有发生,请开始输入

nothing happens, start typing

Today is a really nice day, $|$

什么都没有,但是现在当我开始输入

still nothing, but now when I start typing

Today is a really nice day, $someText|$

在这些美元符号之间的每个字母,警告框都应跳出.

alert box should jump out for each letter in between those dollar signs.

我为什么需要这种功能?每当用户开始键入自己的方程式时,我都希望进行实时方程式预览(MathJax渲染),我可以通过$$符号来识别它是一个方程式(介于两者之间的所有内容都将被渲染).

Why do I need that kind of feature? I want live-equation-preview (MathJax rendering) every time user starts typing his/her equation, and I can recognize it's an equation by $$ signs (everything in between is rendered).

编辑:文本字段中可能有多个$$.脚本必须识别当前处于活动状态的光标(光标位置在其$$之间).

Multiple $$'s are possible in textfield. Script must recognize the one which is currently active (cursor position is between it's $$'s).

推荐答案

您可以使用jQuery插入符

You can use jQuery caret plugin

http://examplet.buss.hk/download/download. php?plugin = caret.1.01

注意:编辑并删除第8行中的这些字符Ôªø

replace功能. (不替换任何东西,而是使用匹配模式的索引)

and replace funcition. ( not to replace anything but to use the index of matched pattern )

$("#myText").bind("keyup", function(e){
     var text = $(this).val();
     var caret = $(this).caret().start;

     if(text && text.length > 0){
        text.replace(/\$.*?\$/g, function(m, n){
           if(caret > n && caret < (n + m.length)){
              alert("BOOM");
           }
        });
     }
});

演示: http://jsfiddle.net/xqXXb/

仅显示如何执行此操作.您可以改善这一点.

Just showing how to do it. You can improve this.

使用

$("#myText").bind("keyup keydown change", function(e){ ...

以获得更好的结果.

这篇关于在两个$$符号之间书写时识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆