¡¤Äúµ±Ç°µÄλÖãºÊ×Ò³ > ¼¼Êõ½Ì³Ì > JavaScript >

[JS]¼æÈÝIE getElementsByClassNameÈ¡±êÇ©

ʱ¼ä:2014-08-14 12:21°¤ÌßÇ°¶Ë
Ò»¸öÅóÓÑÔÚij¹«Ë¾ÎªÄ³¹úÆó½ÌÓý»ú¹¹×öÍøÂç¿Î³Ì¡£ËýµÄÈÎÎñÊÇ×öһЩǰ¶ËÒ³ÃæµÄ¹¤×÷£¬µ«×öÍøÂç¿Î³ÌµÄÒªÇóÊÇtable²¼¾Ö£¬Çҵÿ¼ÂÇie6¡¢7¡¢8£¬²»ÔÊÐíÓÃjqueryµÈ¿ò¼Ü¡£ÒªÖªµÀieÊDz»Ö§³ÖclassNameÈ¡±êÇ©µÄ°¡£¬Èç¹ûҪͨ¹ýclassName»ñÈ¡±êÇ©Ôõô°ì°¡¡£Äã¿ÉÒÔͨ¹ýÕâ¸ö·½·¨

Ò»¸öÅóÓÑÔÚij¹«Ë¾ÎªÄ³¹úÆó½ÌÓý»ú¹¹×öÍøÂç¿Î³Ì¡£ËýµÄÈÎÎñÊÇ×öһЩǰ¶ËÒ³ÃæµÄ¹¤×÷£¬µ«×öÍøÂç¿Î³ÌµÄÒªÇóÊÇtable²¼¾Ö£¬Çҵÿ¼ÂÇie6¡¢7¡¢8£¬²»ÔÊÐíÓà jqueryµÈ¿ò¼Ü¡£ÒªÖªµÀieÊDz»Ö§³ÖclassNameÈ¡±êÇ©µÄ°¡£¬Èç¹ûҪͨ¹ýclassName»ñÈ¡±êÇ©Ôõô°ì°¡¡£Äã¿ÉÒÔͨ¹ýÕâ¸ö·½·¨£º

  1. function getElementsByClassName(className,root,tagName) {    //root£º¸¸½Úµã£¬tagName£º¸Ã½ÚµãµÄ±êÇ©Ãû¡£ ÕâÁ½¸ö²ÎÊý¾ù¿ÉÓпÉÎÞ 
  2.     if(root){ 
  3.         root=typeof root=="string" ? document.getElementById(root) : root;    
  4.     }else{ 
  5.         root=document.body; 
  6.     } 
  7.     tagNametagName=tagName||"*";                                     
  8.     if (document.getElementsByClassName) {                    //Èç¹ûä¯ÀÀÆ÷Ö§³ÖgetElementsByClassName£¬¾ÍÖ±½ÓµÄÓà
  9.         return root.getElementsByClassName(className); 
  10.     }else {  
  11.         var tagroot.getElementsByTagName(tagName);    //»ñÈ¡Ö¸¶¨ÔªËØ 
  12.         var tagAll = [];                                    //ÓÃÓÚ´æ´¢·ûºÏÌõ¼þµÄÔªËØ 
  13.         for (var i = 0; i < tag.length; i++) {                //±éÀú»ñµÃµÄÔªËØ 
  14.             for(var j=0,n=tag[i].className.split(' ');j<n.length;j++){    //±éÀú´ËÔªËØÖÐËùÓÐclassµÄÖµ£¬Èç¹û°üº¬Ö¸¶¨µÄÀàÃû£¬¾Í¸³Öµ¸øtagnameAll 
  15.                 if(n[j]==className){ 
  16.                     tagAll.push(tag[i]); 
  17.                     break; 
  18.                 } 
  19.             } 
  20.         } 
  21.         return tagAll; 
  22.     } 

 

ÈÈÃÅÎÄÕÂÍƼö

ÇëÉÔºò...

±£ÀûÍþÊÓÔÆƽ̨-ÇáËÉʵÏֵ㲥ֱ²¥ÊÓƵӦÓÃ

¿á²¥ÔÆÊý¾Ýͳ¼Æ·ÖÎö¿çƽ̨²¥·ÅÆ÷