将表插入内容可编辑div

Bla*_*umb 14 javascript jquery html-table rangy

我有一个小提示,显示我的代码正在做什么.使用javascript/jquery我试图将一个表插入到当前插入位置的内容可编辑div中.我正在使用Tim Down的Rangy库来实现这一目标.我用以下javascript做这个.

var range = getFirstRange();
var el = document.createElement("table");
var tableHtml = "";
for (var a = 0; a <= tableY; a++) {
    if(a%2==0){
       tableHtml += '<tr class="zebra">';
    }
    else{
       tableHtml += '<tr>';
    }
    for (var b = 0; b <= tableX; b++) {
       tableHtml += '<td>&nbsp;</td>';
    }
    tableHtml += '</tr>';   
}
$(el).html(tableHtml); 
range.insertNode(el);
rangy.getSelection().setSingleRange(range);
Run Code Online (Sandbox Code Playgroud)

以防万一它是getFirstRange函数.

function getFirstRange() {
   var sel = rangy.getSelection();
   return sel.rangeCount ? sel.getRangeAt(0) : null;
} 
Run Code Online (Sandbox Code Playgroud)

我需要在放置此表的任何地方制作有效的html.例如,如果插入符号位于链接的中间,我试图避免以下html.

<p>some text <a href="#">text 
                         <table>
                             <tr>
                               <td>table content</td>
                             </tr>
                         </table> 
              text</a> more text</p> 
Run Code Online (Sandbox Code Playgroud)

我希望它看起来像这样.

<p>some text <a href="#">text</a></p>
<table>
   <tr>
     <td>table content</td>
   </tr>
</table>
<p><a href="#">text</a> more text</p>
Run Code Online (Sandbox Code Playgroud)

svi*_*gen 3

如果要在无法有效包含新节点的选定节点之后立即删除新节点,请替换以下内容:

range.insertNode(el);
Run Code Online (Sandbox Code Playgroud)

像这样事情

var badNodes = {a: 1, p: 1};

// starting with the node at the beginning of the range,
// iterate to the "left" until we find a node that isn't
// a text node
var n = range.startContainer;
var tag = n.nodeName;
while (tag == '#text') {
    n = n.parentNode;
    tag = n.nodeName;
}

// if the node we landed on isn't one of our bad nodes ...
if (badNodes[tag.toLowerCase()]) {

    // that we refuse to insert 'el' into, continue iterating to the
    // "left" until we find a node we're willing to place 'el' after.
    while (badNodes[n.parentNode.nodeName.toLowerCase()]) {
        n = n.parentNode;
        tag = n.nodeName;
    }
    n.parentNode.insertBefore(el, n.nextSibling);

} else {
    range.insertNode(el);
}
Run Code Online (Sandbox Code Playgroud)

查看我的小提琴叉:http://jsfiddle.net/zntwL/29/


更新 (我认为这就是你想要的)

如果您想拆分无效节点并放入新节点,请使用类似以下内容:

var badNodes = {a: 1, p: 1};

// starting with the node at the beginning of the range,
// iterate to the "left" until we find a node that isn't
// a text node
var n = range.startContainer;
var tag = n.nodeName;
while (tag == '#text') {
    n = n.parentNode;
    tag = n.nodeName;
}

// if the node we landed on is one of our "bad" nodes ...
if (badNodes[tag.toLowerCase()]) {

    // continue iterating to the "left" until we find a "good" node
    while (badNodes[n.parentNode.nodeName.toLowerCase()]) {
        n = n.parentNode;
        tag = n.nodeName;
    }

    // remove everything from our "good" node from the start of the
    // range to the end of the node. this causes all bad nodes to be
    // severed and auto-closed and auto-opened as necessary at the cut.
    range.setEndAfter(n);
    var clipped = range.extractContents();

    // drop 'el' in after the break (right where we want it)
    n.parentNode.insertBefore(el, n.nextSibling);

    // and re-attach the clipped portion of the "good" node, which
    // includes the auto-opened "bad" nodes.
    el.parentNode.insertBefore(clipped, el.nextSibling);

} else {
    range.insertNode(el);
}
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/zntwL/31/

您的最终解决方案可能需要一些调整。您可能需要以不同的方式检测 #text 节点以实现跨浏览器兼容。您需要对其进行模块化并badNodes适当地填充数组。但是,我认为这是总体想法。