柳暗花明笔记

    jquery 在光标后插入html插件insertAtCaret

    所属分类:[ php&web ] 猫哲学家 发表于:2017-03-01 12:29 浏览:218

    ;(function($) {

    $.fn.insertAtCaret = function (myValue) {

    return this.each(function() {

    //IE support
    if (document.selection) {

    this.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
    this.focus();

    } else if (this.selectionStart || this.selectionStart == '0') {

    //MOZILLA / NETSCAPE support
    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();
    }
    });
    };

    })(jQuery);

    调用方法:

    $('#myID').insertAtCaret("4");

    示例:

    http://stackoverflow.com/questions/1064089/inserting-a-text-where-cursor-is-using-javascript-jquery/1064139#1064139

    标签: php 
    上一编文章 如果字符串里有PHP_EOL,unserialize会报错 Notice: unserialize(): Error at offset 300 of 732 bytes 下一编文章 php 闭包学习小例子 & array_filter、array_map、array_walk数组函数的妙用
    最近文章
    发表评论