首页 > 语言 > JavaScript > 正文

jquery实现在光标位置插入内容的方法

2024-05-06 14:46:18
字体:
来源:转载
供稿:网友

本文实例讲述了jquery实现在光标位置插入内容的方法。分享给大家供大家参考。具体实现方法如下:
代码如下:<!DOCTYPE html> 
<html> 
<head> 
    <title>无标题页</title> 
    <script type="text/javascript" src="jquery-1.7.2.min。js"></script> 
    <script type="text/javascript"> 
        (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(); 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选