demo效果如下:

html代码部分如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>js获取编辑框游标的位置</title>
<script src="http://www.w3school.com.cn/jquery/jquery-1.11.1.min.js" type="text/javascript"></script> <style type="text/css">
#Div1, #Div2
{
width: 500px;
height: 100px;
border: solid 1px black;
}
</style>
</head>
<body>
<button type="button" id="ddddd" >
插入字符
</button>
<div contentEditable="true" style="height:50px; border:2px solid red;" id="test">
dasdsadasdasdasd<img src="http://www.cnblogs.com/images/xml.gif" alt="订阅">dasdasdasdasdsadsa
</div> </body>
</html>
<script>
$('#ddddd').click(function(){
document.getElementById('test').focus();
insertHtmlAtCaret('<img src="http://www.cnblogs.com/images/xml.gif" />');
})
function insertHtmlAtCaret(html) {
var sel, range;
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
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);
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);
}
}
</script>

js 指定位置插入html标签(可编辑div)的更多相关文章

  1. 在一段字符串中的指定位置插入html标签,实现内容修改留痕

    客户需求:实现内容修改留痕,并且鼠标移动到元素时,显示修改人和修改时间. (其实呢本人觉得这个如果是静态的页面,或者是后端拼接好的html,都很好实现,如果让前端动态实现就......) 前端实现的方 ...

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

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

  3. 数组的操作。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 < ...

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

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

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

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

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

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

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

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

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

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

  9. Java 替换word文档文字,指定位置插入图片

    先说下 需要的依赖包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ex ...

随机推荐

  1. [ACM] HDU 2295 Radar (二分法+DLX 重复覆盖)

    Radar Problem Description N cities of the Java Kingdom need to be covered by radars for being in a s ...

  2. cocos2d-html5基金会

    1 环境结构 版本号Cocos2d-html5-v2.2,tomcat7.0 构造tomcat.然后直接解压Cocos2d-html5-v2.2.zip.解压后根文件访问的文件夹index.html你 ...

  3. Docker contanier comunication with route

    2台宿主机,hslave1 192.168.1.153, hslave2 192.168.1.154 修改网段 docker0默认网段是172.17.0.0/16,修改154机器的docker0的网段 ...

  4. boxfilter 实现

    A implementation of boxfilter boxfilter 是均值滤波的一种改进.在以下这篇blog里面有介绍. http://www.cnblogs.com/easymind22 ...

  5. CodeForces 277A Learning Languages (并检查集合)

    A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes The "Be ...

  6. Oracle数据库之开发PL/SQL子程序和包

    Oracle数据库之开发PL/SQL子程序和包   PL/SQL块分为匿名块与命名块,命名块又包含子程序.包和触发器. 过程和函数统称为PL/SQL子程序,我们可以将商业逻辑.企业规则写成过程或函数保 ...

  7. MAC OSX在视图port哪个程序占用,杀死进程的方法

    sudo lsof -i :9000 COMMAND   PID    USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME java    6 ...

  8. ACM 入门计划

    acm 本文由swellspirit贡献 ACM • I can accept failure. but I can't accept not trying. Life is often compar ...

  9. div元素上下左右居中

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  10. php禁用一些重要功能

    passthru() 功能叙述性说明:我们同意将运行外部程序和回音输出.分类似至 exec(). 临界水平:高 exec() 功能叙述性说明:同意运行外部程序(例如 UNIX Shell 要么 CMD ...