我有一个问题。我一直在试图解决这一问题现在一段时间,我已经准备好到爆炸。这是我的要求:
我有一个外部工具栏(部分不是唯)所述编辑,我要用于插入HTML标签。用户应当能够点击的链接在工具条之后,一些事情可能发生以下情况:

  1. 如果有任何选案文,这一案文得到包裹到HTML标签
  2. 如果没有选定的文本,一个空HTML标是插在编辑器
  3. 不管的情况下,标必须放在新的元素,因此,当用户输入更多的文本,它所在的新的元素

的功能是非常相似的紧迫"B"或"U"的按钮上编辑工具栏(现在,我用这个编辑,它也确实很好:-)).它保留了一切都很好。到目前为止,我能够做到1或2个,但不是3.步骤3是非常重要的,因为没有它,用户的经验在很大程度上受到影响。我真的需要你的帮助来实现它。以下是一个简化版本的方法,执行插入(只是插入DIV为了简明起见).这一点。_oEditor-当地的实例锐编辑:

this._insertElement = function() {
var sSelection = this._oEditor._getSelection(); // Attempt to get selected text from the editor
if (sSelection == '') sSelection = ' '; // If nothing was selected, insert a non-breaking space

var sNewElt = '<div>' + sSelection + '</div>';

this._oEditor.execCommand('inserthtml', sNewElt);

this._oEditor.focus(); // This gives the editor focus, but places cursor in the beginning!
}

它是什么,我必须做到将光标放在正确位置吗?它甚至有可能吗?此外,如果有一个更好的方式实现这一点,我完全支持它。谢谢你!

有帮助吗?

解决方案

这是完整的解决方案:

this._insertElement = function() {
   var sSelection = this._oEditor._getSelection(); 
  if (sSelection == '') sSelection = ' '; 

  var sNewElt = '<div>' + sSelection + '</div>';

  this._oEditor.execCommand('inserthtml', sNewElt);

  var pos = 1000; //pos determines where to place the cursor. if greater than the length of characters, it will place at the end.
  if(this._oEditor.createTextRange) { //IE Specific code
        var range = this._oEditor.createTextRange();   
        range.move("character", pos);   
        range.select();   
    } else if(this._oEditor.selectionStart) {  //Works with Firefox and other browsers 

        this._oEditor.focus();   
        this._oEditor.setSelectionRange(pos, pos);   
  }  
  this._oEditor.focus(); 
}

其他提示

将光标需要不同的方法用于不同的浏览器。与即只要创建一个 TextRange 目的,在Safari你可以使用的窗口。找到()或 选择 对象,其/safari/铬需要的又一方法。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top