Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

javascript - Get content between comments

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...