2010-10-27 62 views
1

我需要在應用程序中的光標位置添加跨度。我可以使用以下代碼在插入位置添加跨度,但不能在插入位置放置插入符號,因此,如果用戶鍵入內容,它將進入新跨度。位置光標/光標位於新添加的跨度中,位於小型mce中

marker = ed.selection.getBookmark(); 
ed.selection.moveToBookmark(marker); 
tinyMCE.execCommand('mceInsertContent',false,'<span id="mytitle"></span>'); 
ed.selection.moveToBookmark(marker); 

回答

1

我自己花了一些時間來放置一個光標。您可能需要稍微修改它,以便與您創建的跨度一起使用,但是這應該使您的方向朝向正確的方向:

// sets the cursor position to the defined node 
// ed: editor, start: defines if the cursor is to be placed at the start or end of the node 
// return node: boolean, if set returns the caretnode instead of deleting it 
function setCursor(ed, node, start, return_node){ 

    tn = ed.getDoc().createTextNode("."); 
    if (start){ 
    node.insertBefore(tn, node.firstChild); 
    } 
    else node.appendChild(tn); 

    rng = ed.selection.getRng(); 
    rng.selectNode(tn); 
    rng.setStartBefore(tn); 
    rng.setStartAfter(tn); 

    ed.selection.setRng(rng); 

    if (return_node) return tn; 

    node.removeChild(tn); 
}