2012-02-23 90 views
1

這裏的html:按標籤由標籤裏和周圍組裏替換每個標籤p UL

<section class="category"> 
     <p><a href="product-list.html">Lorem ipsum dolor sed diam</a></p> 
     <p><a href="product-list.html">Lorem ipsum dolor sed diam</a></p> 
     <p><a href="product-list.html">Lorem ipsum dolor sed diam</a></p> 
     <p><a href="product-list.html">Lorem ipsum dolor sed diam</a></p> 
     <section class="clear"></section> 
    </section> 

如何更換標籤p - >李和UL標籤包裹組裏。我需要的是:

<section class="category"> 
     <ul> 
     <li><a href="product-list.html">Lorem ipsum dolor sed diam</a></li> 
     <li><a href="product-list.html">Lorem ipsum dolor sed diam</a></li> 
     <li><a href="product-list.html">Lorem ipsum dolor sed diam</a></li> 
     <li><a href="product-list.html" >Lorem ipsum dolor sed diam</a></li> 
     </ul> 
     <section class="clear"></section> 
</section> 
+1

提示的組合:有沒有需要有明確的元素('section.clear'),只是使用其中一種簡單的clearfix解決方案(在父級上是'overflow:hidden;'就足夠了)。另外,如果你堅持使用html元素來清除浮動元素,爲什麼要使用語義'section'元素? 'section'不是在這裏去''div',它在這裏爲我們過去使用div的一些東西提供了意義。然而,在清除元素的情況下,'section'不適合,'div'是。 – powerbuoy 2012-02-23 09:58:07

回答

0
jQuery('.category').each(function(){ 
    var c = jQuery(this); 
    var ul = jQuery('<ul/>'); 
    c.find('p').each(function(){ 
     var p = jQuery(this); 
     ul.append(jQuery('<li/>').append(p.children())); 
    }).remove(); 
    c.prepend(ul);   
}); 
0

在你的jQuery做到這一點

$("p").each(function() { 
$(this).replaceWith("<li>" + $(this).html() + "</li>"); 
    }); 

而且提高你的錄取率。