我找到了 JQuery InsertAtCaret 函数Here https://gist.github.com/mathiasbynens/326491但没有详细说明如何使用它。我已经尝试了很多方法来了解它如何使用,但找不到任何方法。这是函数。
$.fn.insertAtCaret = function(myValue) {
return this.each(function() {
var me = this;
if (document.selection) { // IE
me.focus();
sel = document.selection.createRange();
sel.text = myValue;
me.focus();
} else if (me.selectionStart || me.selectionStart == '0') { // Real browsers
var startPos = me.selectionStart, endPos = me.selectionEnd, scrollTop = me.scrollTop;
me.value = me.value.substring(0, startPos) + myValue + me.value.substring(endPos, me.value.length);
me.focus();
me.selectionStart = startPos + myValue.length;
me.selectionEnd = startPos + myValue.length;
me.scrollTop = scrollTop;
} else {
me.value += myValue;
me.focus();
}
});
};
我有一个文本框输入字段及其下方的文本区域。我应该在哪里调用这个函数以及我应该给它什么值。我必须在哪里提供我的文本区域的参考。
这是上述插件的修改版本:
jQuery.fn.extend({
insertAtCaret: function(myValue){
return this.each(function(i) {
if (document.selection) {
//For browsers like Internet Explorer
this.focus();
sel = document.selection.createRange();
sel.text = myValue;
this.focus();
}
else if (this.selectionStart || this.selectionStart == '0') {
//For browsers like Firefox and Webkit based
var startPos = this.selectionStart;
var endPos = this.selectionEnd;
var scrollTop = this.scrollTop;
this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
this.focus();
this.selectionStart = startPos + myValue.length;
this.selectionEnd = startPos + myValue.length;
this.scrollTop = scrollTop;
} else {
this.value += myValue;
this.focus();
}
})
}
});
基本上,这个插件允许您在多个插入符处插入一段文本textbox
or textarea
。你可以这样使用它:
$('#element1, #element2, #element3, .class-of-elements').insertAtCaret('text');
Working Demo http://jsfiddle.net/gHN8G/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)