Html中的comment其实也有nodeType的,只是我们一直不用罢了。它的nodeType = 8哦。于是,就有了一个基于jQuery的插件:comments
它来自:http://www.bennadel.com/blog/1563-jQuery-Comments-Plug-in-To-Access-HTML-Comments-For-DOM-Templating.htm
<--- --------------------------------------------------------------------------------------- ----
Blog Entry:
jQuery Comments() Plug-in To Access HTML Comments For DOM Templating
Author:
Ben Nadel / Kinky Solutions
Link:
http://www.bennadel.com/index.cfm?event=blog.view&id=1563
Date Posted:
Apr 14, 2009 at 7:01 PM
---- --------------------------------------------------------------------------------------- --->
// This jQuery plugin will gather the comments within
// the current jQuery collection, returning all the
// comments in a new jQuery collection.
//
// NOTE: Comments are wrapped in DIV tags.
jQuery.fn.comments = function( blnDeep ){
var blnDeep = (blnDeep || false);
var jComments = $( [] );
// Loop over each node to search its children for
// comment nodes and element nodes (if deep search).
this.each(
function( intI, objNode ){
var objChildNode = objNode.firstChild;
var strParentID = $( this ).attr( "id" );
// Keep looping over the top-level children
// while we have a node to examine.
while (objChildNode){
// Check to see if this node is a comment.
if (objChildNode.nodeType === 8){
// We found a comment node. Add it to
// the nodes collection wrapped in a
// DIV (as we may have HTML).
jComments = jComments.add(
"<div rel='" + strParentID + "'>" +
objChildNode.nodeValue +
"</div>"
);
} else if (
blnDeep &&
(objChildNode.nodeType === 1)
) {
// Traverse this node deeply.
jComments = jComments.add(
$( objChildNode ).comments( true )
);
}
// Move to the next sibling.
objChildNode = objChildNode.nextSibling;
}
}
);
// Return the jQuery comments collection.
return( jComments );
}
代码其实并不多,大概知道就好,本来是用phpQuery也这样写的。但发现。。。stick的时候,即使我选择了nodeType=8也取不到值。我晕啊,最后我只能用正则将注释取出来当成内容用了。
早先有人把代码扔在textarea里,用来当成数据加载,现在也有人开始无耻的利用注释了。真不厚道

评论 (0)
暂无评论