可用于示例:
<html>
<head></head>
<body>
<div>something</div>
<!-- id:321312321 -->
<div>Replace all within this comment space</div>
<div>1</div>
<div>2</div>
<div>3</div>
<!-- id:321312321 -->
</body>
</html>
作为:
commentReplaceBetween("<div>1</div>", " id:321312321 ");
将导致:
<html>
<head></head>
<body>
<div>something</div>
<!-- id:321312321 -->
<div>1</div>
<!-- id:321312321 -->
</body>
</html>
代码:
function commentReplaceBetween(template, commentContent) {
var div = document.createElement("div");
div.innerHTML = template;
// Find our comment first
var comment = nodeCommentFind(document.documentElement, commentContent);
var parentNode = comment.parentNode;
var sibling = comment.nextSibling;
// Delete all contents in between
while ( sibling ) {
if ( nodeCommentContentMatch(sibling, commentContent) ) {
break;
} else {
var tmp = sibling;
sibling = sibling.nextSibling;
parentNode.removeChild(tmp);
}
}
// Then append our elements afterw
var child, children = div.childNodes;
for ( var i = 0; i < children.length; i++) {
child = children[i];
// See:
// http://www.javascriptkit.com/domref/nodetype.shtml
// and
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms764649(v=vs.85).aspx
if ( child.nodeType === 1 || child.nodeType === 7 || child.nodeType === 8 || child.nodeType === 11 ) {
// Sibling here should be the last comment, otherwise the entire page have been incorrectly deleted.
parentNode.insertBefore(child, sibling);
}
}
}
var cacheComment = {};
function nodeCommentFind(node, commentContent) {
var child = cacheComment[commentContent];
if ( child ) {
return child;
}
else {
return nodeCommentFindRecurse(node, commentContent);
}
}
function nodeCommentFindRecurse(node, commentContent) {
var children = node.childNodes;
var child, returns;
for(var i = 0; i < children.length; i++) {
child = children[i];
if( nodeCommentContentMatch(child, commentContent) ) {
cacheComment[commentContent] = child;
return child;
}
else if ( child.hasChildNodes && child.childNodes ) {
returns = nodeCommentFindRecurse(child, commentContent);
if ( returns ) return returns;
}
}
}
function nodeCommentIs(node) {
return node.nodeType === 8;
}
function nodeCommentContentMatch(node, commentContent) {
return nodeCommentIs(node) && node.nodeValue === commentContent;
}
/////////////////////////////////////////////////////////////////////
////////////////////////// BONUS INFORMATION ////////////////////////
/////////////////////////////////////////////////////////////////////
我的commentReplaceBetween 是这样调用的
commentReplaceBetween(commentContent)(template);
所以我的实际commentReplaceBetween 函数被包装起来,看起来像这样:
function commentReplaceBetween(commentContent) {
return function(template) {
var div = document.createElement("div");
div.innerHTML = template;
// Find our comment first
var comment = nodeCommentFind(document.documentElement, commentContent);
var parentNode = comment.parentNode;
var sibling = comment.nextSibling;
// Delete all contents in between
while ( sibling ) {
if ( nodeCommentContentMatch(sibling, commentContent) ) {
break;
} else {
var tmp = sibling;
sibling = sibling.nextSibling;
parentNode.removeChild(tmp);
}
}
// Then append our elements afterw
var child, children = div.childNodes;
for ( var i = 0; i < children.length; i++) {
child = children[i];
// See:
// http://www.javascriptkit.com/domref/nodetype.shtml
// and
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms764649(v=vs.85).aspx
if ( child.nodeType === 1 || child.nodeType === 7 || child.nodeType === 8 || child.nodeType === 11 ) {
// Sibling here should be the last comment, otherwise the entire page have been incorrectly deleted.
parentNode.insertBefore(child, sibling);
}
}
};
}