修改一个编辑器需要用的到range和selection对象
2013-04-11 00:17
363 查看
除了浏览器兼容麻烦点,其余的都是大赞啊,附下一些代码可以起手:
详细资料可以查看网站: 里面讲的非常详细。
http://help.dottoro.com/ljehhofi.php Mozilla和IE都有
https://developer.mozilla.org/en-US/docs/DOM/Selection Mozilla的资料
http://msdn.microsoft.com/en-us/library/ie/hh772124(v=vs.85).aspx IE的资料
<html> <head> <script type="text/javascript"> function MoveButton () { var wanderer = document.getElementById ("wanderer"); if (window.getSelection) { // all browsers, except IE before version 9 var selection = window.getSelection (); if (selection.rangeCount > 0) { var range = selection.getRangeAt (0); range.collapse (false); range.insertNode (wanderer); } } else { // Internet Explorer before version 9 var textRange = document.selection.createRange (); textRange.collapse (false); textRange.pasteHTML (wanderer.outerHTML); wanderer.parentNode.removeChild (wanderer); } } function insertHtmlAtSelectionEnd(html, isBefore) { var sel, range, node; if (window.getSelection) { sel = window.getSelection(); if (sel.getRangeAt && sel.rangeCount) { range = window.getSelection().getRangeAt(0); range.collapse(isBefore); // Range.createContextualFragment() would be useful here but was // until recently 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); } } else if (document.selection && document.selection.createRange) { range = document.selection.createRange(); range.collapse(isBefore); range.pasteHTML(html); } } </script> </head> <body> <div onmouseup="MoveButton ()" style="width:400px; background-color:#e0f0d0;"> Select some text with your mouse within this field. When the left button is released the wanderer button is placed at the ending of the selection. Left mouse clicks also move the wanderer button in Internet Explorer, Firefox, Google Chrome and Safari. </div> <button id="wanderer">wanderer</button> <br/><br/><br/><br/> <input type="button" onclick="insertHtmlAtSelectionEnd('[BEFORE]', true)" value="Insert before"> <input type="button" onclick="insertHtmlAtSelectionEnd('[AFTER]', false)" value="Insert after"> <div contenteditable="true">Some editable text. Select some and press on of the buttons above.</div> </body> </html>
详细资料可以查看网站: 里面讲的非常详细。
http://help.dottoro.com/ljehhofi.php Mozilla和IE都有
https://developer.mozilla.org/en-US/docs/DOM/Selection Mozilla的资料
http://msdn.microsoft.com/en-us/library/ie/hh772124(v=vs.85).aspx IE的资料
相关文章推荐
- C++实现线程同步的几种方式 线程同步是指同一进程中的多个线程互相协调工作从而达到一致性。之所以需要线程同步,是因为多个线程同时对一个数据对象进行修改操作时,可能会对数据造成破坏,下面是多
- 如何编程修改一个隶属于视图的对象颜色
- 写一个递归方法,传入一个Map,返回这个Map对象中一共有多个Map类型的对象 Map中是可以添加Map,被添加的这个Map也可以添加Map,所以需要判断传入的Map对象的所有子节点,如果是Map就
- spring mvc返回json字符串数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable
- ALTER TABLE 修改表时 因为有一个或多个对象访问此列
- JAVA中对于需要频繁new的对象的一个优化的方法
- springmvc中前台Date类型直接传到后台需要添加一个时间属性编辑器
- 有一个由大小写组成的字符串,现在需要对他进行修改,将其中的所有小写字母排在大写字母的前面
- JS如何封装一些列方法为一个对象的操作,然后集中管理这些操作,方便修改和调用
- document.selection.createRange方法----获取选择对象文本
- 关于Excel操作编写的一个软件设计构思案例[连载] --如何打开Excel文件,获取需要列的数据显示到表格内做修改
- C# 在两个不同的方法里面Lock同一个锁对象,是否需要线程等待?
- Spring中如何获取和重置一个BeanFactory对象?或者说,如何动态修改Hibernate Configuration?
- 需要求三个长方体的体积,请编写一个基于对象的程序
- 修改SQL数据库中表字段类型时,报“一个或多个对象访问此列”错误的解决方法
- 迭代器-迭代对象-dir(a)可以查看该数据类型有多少种方法。range(10)在py3里就是一个迭代器,for循环实际就是迭代器的应用
- 浏览器Range,Selection等选中文本对象
- 在SQL2000中创建或修改列的默认值 需要注意的一个参数
- 让一个对象=另一个对象,修改一个对象的值,如何不影响另一个的值
- ip变动时 wordpress 需要修改一个东西。