方法:

function insertHtmlAtCaret(html) {
var sel, range;
if (window.getSelection) {
// IE9 and non-IE
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
// Range.createContextualFragment() would be useful here but is
// non-standard and not supported in all browsers (IE9, for one)
var el = document.createElement("div");
el.innerHTML = html;
var frag = document.createDocumentFragment(), node, lastNode;
while ( (node = el.firstChild) ) {
lastNode = frag.appendChild(node);
}
range.insertNode(frag);
// Preserve the selection
if (lastNode) {
range = range.cloneRange();
range.setStartAfter(lastNode);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
}
}
} else if (document.selection && document.selection.type != "Control") {
// IE < 9
document.selection.createRange().pasteHTML(html);
}
} 调用方式:
var content = '你好啊,我是新插入的内容数据';
$('.js_content').focus();//要插入内容的div
insertHtmlAtCaret(content)

JS向光标指定位置插入内容的更多相关文章

  1. 利用RandomAccessFile类在指定文件指定位置插入内容

    package File; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; ...

  2. Java分享笔记:RandomAccessFile流 & 在文件指定位置插入内容

    RandomAccessFile流:随机存取文件流,该类定义了一个记录指针,通过移动指针可以访问文件的任意位置,且对文件既可以读也可以写.使用该类的write方法对文件写入时,实际上是一种覆盖效果,即 ...

  3. js 在光标位置插入内容

    原文:https://blog.csdn.net/smartsmile2012/article/details/53642082 createDocumentFragment()用法: https:/ ...

  4. 如何在JS数组特定索引处指定位置插入元素?

    如何在JS数组特定索引处指定位置插入元素? 需求: 将一个元素插入到现有数组的特定索引处.听起来很容易和常见,但需要一点时间来研究它. // 原来的数组var array = ["one&q ...

  5. 数组的操作。1,JS数组去重。2,把数组中存在的某个值,全部找出来。3在JS数组指定位置插入元素。。。

    1,数组去重 let arr = [1,2,3,4,5,6,1,2,3,'a','b','a']; let temp = []; // 作为存储新数组使用 for(let i = 0; i < ...

  6. 正确截取List指定位置的内容

    正确截取List指定位置的内容 import java.util.ArrayList; import java.util.List; public class ListUtils { public s ...

  7. PHP字符串指定位置插入字符串

    1.substr_replace(string,replacement,start,length);需插入时设置length为0即可 string 必需.规定要检查的字符串. replacement ...

  8. java格式化字符串,在指定位置插入指定字符串,兼容中英文以及特殊字符,例如:换行,用于解决生成pdf换行问题等问题

    本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处.  http://www.cnblogs.com/king-xg/p/6370890.html 如果觉得对您有 ...

  9. 【PHP】在目标字符串指定位置插入字符串

    PHP如何在指定位置插入相关字符串,例子:123456789变为1_23_456789插入"_"到指定的位置! (可以用作换行或者其他处理) 插入示例,具体思路在代码中有注释: & ...

随机推荐

  1. iphone field test 源码

    Iphone工程模式读取周围BTS信息的路测程序:包括后台和界面.-iphone field test, used for reading the BTS infomation nearby. 下载地 ...

  2. SVN错误:run 'cleanup' if it was interrupted的解决

    原文转自:http://www.lxway.com/812960411.htm 今天碰到了个郁闷的问题,svn执行clean up命令时报错“Previous operation has not fi ...

  3. Windows安装包制作指南——Advanced Installer的使用

    1. 前言 最近需要制作windows的安装包,据说Advanced Installer比较强大,遂拿它来制作安装包.在网上少量资料以及官网简约文档中摸索前进,总算是制作出可用的安装包,在此记录,仅供 ...

  4. Linux安装gcc编译器详解

    本人使用的是CentOS 6.5 64位系统,由于在安装系统的时候并没有勾选安装gcc编译器,因此需要自行安装gcc编译器. 使用yum安装gcc 对于配备了yum的Linux发行版而言,安装gcc编 ...

  5. RMAN_学习笔记4_RMAN Virtual Catalog虚拟恢复目录

    2014-01-01 Created By BaoXinjian Thanks and Regards

  6. 异步数据库查询 Z

    Introduction Microsoft .NET 4.5 introduced new "async and await" methods to provide an eas ...

  7. 在 SQL Server 数据库的 WHERE 语句中使用子查询

    这是关于子查询语句的一系列文章中的第三篇.在这篇文章中我们将讨论WHERE语句中的子查询语句.其他的文章讨论了其他语句中的子查询语句. 本次课程中的所有例子都是基于Microsoft SQL Serv ...

  8. 让finder显示路径

    在控制台输入 defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES 重启finder即可.

  9. 48. Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that each ...

  10. Oracle中的表构造导出到word Sql语句

    select * from ( SELECT t1.Table_Name AS "表名称", t3.comments AS "表说明",  t1.Column_ ...