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| |u00A0)+|(s| |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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…