Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hopefully not too tricky this one...

I am trying to get just the context between the <!-- itemtemplate --> comments using javascript (with the jQuery plugin). The result must exclude the comments though. In this case the parent is a table, but it can be anything such as a div

Table

<table id="lvList" class="grid1">
    <tr>
        <th>Name </th>
        <th>Number </th>
        <th>Type </th>
        <th>Account Manager </th>
    </tr>
    <!-- itemtemplate -->
    <tr>
        <td><boundfield output="hyperlink" datafield="name" dataurlfields="id" dataurlformat="details/?id={0}" /></td>
        <td><boundfield output="string" datafield="id" /></td>
        <td><boundfield output="string" datafield="type" /></td>
        <td><boundfield output="string" datafield="accmgr" /></td>
    </tr>
    <!-- itemtemplate -->
</table>

Div

<div id="lvList">
  <!-- itemtemplate -->
     something something something
     <boundfield output="string" datafield="id" />
  <!-- itemtemplate -->
</div>

Thanks to Felix for the idea

function GetTemplate(root, name) {
    var output = "";
    var record = false;
    function iterate(node) {
        var children = node.childNodes;
        for (var i = 0; i < children.length; i++) {
            var child = children[i];
            if (child.nodeType === 3 && child.nodeValue.trim() === "") continue;

            if (child.nodeType === 8 && child.nodeValue.trim() === name) {
                if (!record) { record = true; continue; }
                else { break; }
            }

            if (record)
                output += outerHTML(child);
            else {
                if (child.hasChildNodes)
                    iterate(child);
            }
        }
    }
    iterate(root);
    return output;
};
function ClearTemplate (root, name) {
    var record = false;
    function iterate(node) {
        var children = node.childNodes;
        for (var i = children.length - 1; i >= 0; i--) {
            var child = children[i];
            if (child.nodeType === 8 && child.nodeValue.trim() === name) {
                if (!record) { record = true; continue; }
                else { break; }
            }
            if (record)
                node.removeChild(child);
            else {
                if (child.hasChildNodes)
                    iterate(child);
            }
        }
    }
    iterate(root);
};
function InsertInto (root, name, items) {
    function iterate(node) {
        var children = node.childNodes;
        for (var i = 0; i < children.length; i++) {
            var child = children[i];
            if (child.nodeType === 3 && child.nodeValue.trim() === "") continue;
            if (child.nodeType === 8 && child.nodeValue.trim() === name) {
                for (var n = items.length - 1; n >= 0; n--)
                    child.parentNode.insertBefore(items[n], child.nextSibling);
                break;
            }
            if (child.hasChildNodes)
                iterate(child);
        }
    }
    iterate(root);
};
String.prototype.trim = function () {
    return this.replace(/^(s|&nbsp;|u00A0)+|(s|&nbsp;|u00A0)+$/g, "");
};
function outerHTML(node) {
    return node.outerHTML || 
            (function (n) {
                var div = document.createElement('div');
                div.appendChild(n.cloneNode(true));
                return div.innerHTML;
            })(node);
};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
113 views
Welcome To Ask or Share your Answers For Others

1 Answer

Neither regex nor jQuery: The idea is to iterate over all child nodes and check whether the node is a comment node and if yes, whether it contains a certain string, e.g. itemtemplate.

function extract(node, needle) {
    var children = node.childNodes,
        record = false,
        container = document.createDocumentFragment();

    for(var i = 0, l = children.length; i < l; i++) {
        var child = children[i];

        if(child.nodeType === 8 && child.nodeValue === needle) {
            record = !record;
            continue;
        }   

        if(record) {
            container.appendChild(child.cloneNode(true));
        }       
    }
    return container;
}

Usage:

var nodes = extract(someParent, ' itemtemplate ');

DEMO

I don't know whether this is the best solution for you situation, you don't give any information about the context.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...