zoukankan      html  css  js  c++  java
  • jQuery插件,将内容插入到光标处

    (function ($) {
    	$.fn.extend({
    		insertAtCaret : function (myValue) {
    			var $t = $(this)[0];
    			if (document.selection) {
    				this.focus();
    				sel = document.selection.createRange();
    				sel.text = myValue;
    				this.focus();
    			} else
    				if ($t.selectionStart || $t.selectionStart == '0') {
    					var startPos = $t.selectionStart;
    					var endPos = $t.selectionEnd;
    					var scrollTop = $t.scrollTop;
    					$t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
    					this.focus();
    					$t.selectionStart = startPos + myValue.length;
    					$t.selectionEnd = startPos + myValue.length;
    					$t.scrollTop = scrollTop;
    				} else {
    					this.value += myValue;
    					this.focus();
    				}
    		}
    	})
    })(jQuery);

    //调用方法:
    $("select").insertAtCaret();
  • 相关阅读:
    java练习6
    java练习5
    java练习4
    java练习3
    java练习2
    java练习1
    用代码实现判断字符串的开头和结尾
    语句练习题2
    语句练习题1
    值类型和引用类型的区别
  • 原文地址:https://www.cnblogs.com/kingwell/p/2816575.html
Copyright © 2011-2022 走看看