简体   繁体   English

兰吉-insertNode IE <9不会在插入符处插入

[英]Rangy - insertNode IE < 9 doesn't insert at caret

I am using Rangy for compatibility to insert a node within a WYSIWYG editor (uEditor). 我正在使用Rangy进行兼容性,以便在WYSIWYG编辑器(uEditor)中插入节点。

It works, however in IE < 9 the inserted node is placed at the beginning of the containing element (iframe) instead of at the caret position. 它可以工作,但是在IE <9中,插入的节点位于包含元素(iframe)的开头,而不是插入符号的位置。 This only happens when there is no text selection. 仅当没有文本选择时才会发生这种情况。

I've created a fiddle, it's crude but has the same effects though using a div not the jquery/iframe thingy the uEditor uses. 我已经创建了一个小提琴,它很粗糙,但是具有相同的效果,尽管使用div而不是uEditor使用的jquery / iframe。

Here's the fiddle: http://jsfiddle.net/RvNT3/ 这是小提琴: http : //jsfiddle.net/RvNT3/

And for IE < 9: http://jsfiddle.net/RvNT3/embedded/result/ 对于IE <9: http//jsfiddle.net/RvNT3/embedded/result/

I know the fiddle isn't representative of my actual code, and it may be down to the browser ultimately. 我知道小提琴不能代表我的实际代码,并且最终可能取决于浏览器。 But if there's a way to get it to work (Insert at caret without text selection) that may just do the trick. 但是,如果有一种方法可以使它起作用(在没有文本选择的情况下插入插入符号),则可以解决问题。

fiddle code: 小提琴代码:

<!--HTML-->
  <div id="pageFrame" >
    Some text
  </div>
  <br/>
  <input type="button" id="clickNode" value="Insert Node" />
<!--End HTML-->

//Javascript
var theDiv = document.getElementById('pageFrame');
theDiv.contentEditable = true;

document.getElementById('clickNode').onclick = ( function() {
addTheNode();   
});

function addTheNode()
{
 rangy.init();

 var range = rangy.getSelection().getRangeAt(0);

 alert(range);

 var newNode = document.createElement("code");
            newNode.className = "code";         
            newNode.contentEditable = false;
            newNode.innerHTML = "&nbsp";
            range.insertNode(newNode);                      
}
//End Javascript

/*CSS*/

  body { font-family: verdana; font-size:11px;}

  div { border:1px solid #000000; padding:5px; }

  code { display:block; border:1px solid #ff0000 }    

/*End CSS */

The problem is that the selection is destroyed before the insertNode() call happens. 问题在于,在insertNode()调用发生之前,选择已被销毁。 The click event is too late: the iframe has already lost focus and in IE has also lost the caret position. click事件为时已晚:iframe已经失去焦点,在IE中也失去了插入符号的位置。 You need to do it earlier or prevent the click from moving the focus away from the iframe. 您需要更早地进行操作,或者防止点击使焦点移离iframe。 The following uses the mousedown event instead of click . 下面使用mousedown事件代替click事件。

http://jsfiddle.net/RvNT3/1/embedded/result/ http://jsfiddle.net/RvNT3/1/embedded/result/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM