利用iframe创建的富文本编辑器,其中两个功能思路暂无好的解决办法:
1.用户在编辑器中,选择文字,并选择特殊格式,如“副标题”,我则将所选区域的文字外围添加<div class='副标题对应的ClassName'></div>,execCommand也能勉强实现这个功能,但是每一次execCommand 都可能新增标签,如果当前的格式比较特殊,就很复杂,期待有别的思路或者好的解决办法
2.用户在回车之后,如果当前回车的这段文字(我的回车生成的div)首字是序号形式(1,2,3或者中文一,二,三),则将这个首字的样式修改为加粗。
现在的问题是:(a)获取选区文字,并替换,如果选区的字是重复的就不能实现,能不能获取到选区前后的位置,或者获取到选区所对应的parent父元素
             (b)我怎么获取回车时前一段文字,比如所对应的div标签对象因为是内部使用,暂时只需要兼容火狐和谷歌浏览器。
为什么最多只能给100分啊??编辑器iframe选区

解决方案 »

  1.   

    以下是我寫的iframe編輯器獲取游標位置及選取對象的方法,你可以看看。
    對你的兩個問題都有幫助
    另外,你可以自己看看有關 Range對象的說明getCursorSelect: function(){
    if (this.iframeDoc.selection) {
    this.selection = this.iframeDoc.selection;
    this.range = this.selection.createRange();
    try {
    this.parent = this.range.parentElement();
    } catch (e) {
    return;
    }
    } else {
    try {
    this.selection = this.iframe.contentWindow.getSelection();
    } catch (e) {
    return;
    }
    this.range = this.selection.getRangeAt(0);
    this.parent = this.range.commonAncestorContainer;
    if(this.parent.nodeName == "#text") {
    this.parent = this.parent.parentNode;
    }
    }
    },
      

  2.   


    getCursorSelect: function(){
    if (this.iframeDoc.selection) {
    this.selection = this.iframeDoc.selection;
    this.range = this.selection.createRange();
    try {
    this.parent = this.range.parentElement();
    } catch (e) {
    return;
    }
    } else {
    try {
    this.selection = this.iframe.contentWindow.getSelection();
    } catch (e) {
    return;
    }
    this.range = this.selection.getRangeAt(0);
    this.parent = this.range.commonAncestorContainer;
    if(this.parent.nodeName == "#text") {
    this.parent = this.parent.parentNode;
    }
    }
    },