将插入位置设置为“可满足的”#39;有孩子的div

时间:2016-04-26 15:34:09

标签: javascript jquery html5

我有这样的HTML结构:

<div contenteditable="true">This is some plain, boring content.</div>

我也有这个功能,允许我将插入位置设置到div中我想要的任何位置:

// Move caret to a specific point in a DOM element
function SetCaretPosition(object, pos)
{
    // Get key data
    var el = object.get(0); // Strip inner object from jQuery object
    var range = document.createRange();
    var sel = window.getSelection();

    // Set the range of the DOM element
    range.setStart(el.childNodes[0], pos);
    range.collapse(true);

    // Set the selection point
    sel.removeAllRanges();
    sel.addRange(range);
}

此代码完全正常,直到我开始向div添加子标记(span, b, i, u, strike, sup, sub),例如

<div contenteditable="true">
    This is some <span class="fancy">plain</span>, boring content.
</div>

当这些子标签最终带有自己的子标签时,情况变得更加复杂。

<div contenteditable="true">
    This is some <span class="fancy"><i>plain</i></span>, boring content.
</div>

基本上,当我尝试setStart到高于子标记开头的索引时,IndexSizeError会抛出SetCaretPosition会发生什么。 setStart仅在到达第一个子标记之前有效。

我需要的是SetCaretPosition函数处理未知数量的这些子标记(以及可能是未知数量的嵌套子标记),以便设置位置的工作方式与其中的相同。没有标签。

对于这两点:

<div contenteditable="true">This is some plain, boring content.</div>

和此:

<div contenteditable="true">
    This is <u>some</u> <span class="fancy"><i>plain</i></span>, boring content.
</div>

SetCaretPosition(div, 20);会将插入符号放在&#39; b&#39;在&#39;无聊&#39;。

我需要什么代码?非常感谢!

2 个答案:

答案 0 :(得分:9)

所以,我遇到了同样的问题,并决定快速编写自己的例程,它递归遍历所有子节点并设置位置。 注意这是如何将DOM节点作为参数,而不是像原始帖子那样的jquery对象

// Move caret to a specific point in a DOM element
function SetCaretPosition(el, pos){

    // Loop through all child nodes
    for(var node of el.childNodes){
        if(node.nodeType == 3){ // we have a text node
            if(node.length >= pos){
                // finally add our range
                var range = document.createRange(),
                    sel = window.getSelection();
                range.setStart(node,pos);
                range.collapse(true);
                sel.removeAllRanges();
                sel.addRange(range);
                return -1; // we are done
            }else{
                pos -= node.length;
            }
        }else{
            pos = SetCaretPosition(node,pos);
            if(pos == -1){
                return -1; // no need to finish the for loop
            }
        }
    }
    return pos; // needed because of recursion stuff
}

我希望这会帮助你!

答案 1 :(得分:0)

它只适用于对象文本 childNodes(0)。所以你必须制作它。这不是非常标准的代码,但是有效。目标是(我)的(p)id将输出对象文本。如果它然后它可能会工作。

<div id="editable" contenteditable="true">dddddddddddddddddddddddddddd<p>dd</p>psss<p>dd</p><p>dd</p>
<p>text text text</p>
 </div>
<p id='we'></p>
<button onclick="set_mouse()">focus</button>
 <script>
function set_mouse() {
    var as = document.getElementById("editable");
    el=as.childNodes[1].childNodes[0];//goal is to get ('we') id to write (object Text)
  var range = document.createRange();
     var sel = window.getSelection();
range.setStart(el, 1);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);

document.getElementById("we").innerHTML=el;// see out put of we id
}
</script>
相关问题