zoukankan      html  css  js  c++  java
  • JQuery在光标位置插入内容

    原理很简单,IE下可以通过document.selection.createRange();来实现,而Firefox(火狐)浏览器则需要首先获取光标位置,然后对value进行字符串截取处理。不多说了~直接上我写的一个jQuery在光标位置插入内容插件吧~

    Query在光标位置插入内容插件代码

    (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);
    用方法
    $(selector).insertAtCaret("value");

  • 相关阅读:
    登录认证
    json
    关于优化
    网站资源
    设计模式
    Python
    查兰IP
    Linux命令
    centos7.0KVM虚拟化
    Shell数组
  • 原文地址:https://www.cnblogs.com/top5/p/1760476.html
Copyright © 2011-2022 走看看