jQuery实现的文本在指定位置插入字符或表达式的方法

本文说明了jQuery实现字符或表达式在文本框内指定的位置。分享给你供你参考。具体实现的方法如下:

1。函数的定义

复制代码如下:(函数($){)

fn.extend({美元。

InsertAtCaret:功能(自我价值){

var $ $ =($){ 0 };

如果(文档选择){

This.focus();

SEL = document.selection.createrange();

sel.text =自我价值;

This.focus();

}

其他的

如果($ t.selectionstart | | t.selectionstart美元= = '0'){

VaR StartPos = t.selectionstart美元;

VaR EndPos = t.selectionend美元;

VaR scrollTop = $ t.scrolltop;

t.value = t.value.substring(0美元美元,让美元)+自我价值+ t.value.substring(EndPos,t.value美元。长度);

This.focus();

t.selectionstart美元= Startpos + myvalue.length;

t.selectionend美元= Startpos + myvalue.length;

t.scrolltop美元= scrollTop;

}

{其他

this.value =自我价值;

This.focus();

}

}

})

}(jQuery);

2。调用方法

复制代码代码如下:$(# textareaid ),InsertAtCaret(新表达);

希望本文能对大家的jQuery程序设计有所帮助。