手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表Tag:comment

取得html中的comment的内容

 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

JavaScript代码
  1. <--- --------------------------------------------------------------------------------------- ----  
  2.       
  3.     Blog Entry:  
  4.     jQuery Comments() Plug-in To Access HTML Comments For DOM Templating  
  5.       
  6.     Author:  
  7.     Ben Nadel / Kinky Solutions  
  8.       
  9.     Link:  
  10.     http://www.bennadel.com/index.cfm?event=blog.view&id=1563  
  11.       
  12.     Date Posted:  
  13.     Apr 14, 2009 at 7:01 PM  
  14.       
  15. ---- --------------------------------------------------------------------------------------- --->  
  16.   
  17.   
  18. // This jQuery plugin will gather the comments within  
  19. // the current jQuery collection, returning all the  
  20. // comments in a new jQuery collection.  
  21. //  
  22. // NOTE: Comments are wrapped in DIV tags.  
  23.    
  24. jQuery.fn.comments = function( blnDeep ){  
  25.     var blnDeep = (blnDeep || false);  
  26.     var jComments = $( [] );  
  27.    
  28.     // Loop over each node to search its children for  
  29.     // comment nodes and element nodes (if deep search).  
  30.     this.each(  
  31.         function( intI, objNode ){  
  32.             var objChildNode = objNode.firstChild;  
  33.             var strParentID = $( this ).attr( "id" );  
  34.    
  35.             // Keep looping over the top-level children  
  36.             // while we have a node to examine.  
  37.             while (objChildNode){  
  38.    
  39.                 // Check to see if this node is a comment.  
  40.                 if (objChildNode.nodeType === 8){  
  41.    
  42.                     // We found a comment node. Add it to  
  43.                     // the nodes collection wrapped in a  
  44.                     // DIV (as we may have HTML).  
  45.                     jComments = jComments.add(  
  46.                         "<div rel='" + strParentID + "'>" +  
  47.                         objChildNode.nodeValue +  
  48.                         "</div>"  
  49.                         );  
  50.    
  51.                 } else if (  
  52.                     blnDeep &&  
  53.                     (objChildNode.nodeType === 1)  
  54.                     ) {  
  55.    
  56.                     // Traverse this node deeply.  
  57.                     jComments = jComments.add(  
  58.                         $( objChildNode ).comments( true )  
  59.                         );  
  60.    
  61.                 }  
  62.    
  63.                 // Move to the next sibling.  
  64.                 objChildNode = objChildNode.nextSibling;  
  65.    
  66.             }  
  67.    
  68.         }  
  69.         );  
  70.    
  71.     // Return the jQuery comments collection.  
  72.     return( jComments );  
  73. }  

代码其实并不多,大概知道就好,本来是用phpQuery也这样写的。但发现。。。stick的时候,即使我选择了nodeType=8也取不到值。我晕啊,最后我只能用正则将注释取出来当成内容用了。

早先有人把代码扔在textarea里,用来当成数据加载,现在也有人开始无耻的利用注释了。真不厚道

 

 

Tags: jquery, comment