按标签由标签里和周围组里替换每个标签p UL
问题描述:
这里的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>
答
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);
});
答
在你的jQuery做到这一点
$("p").each(function() {
$(this).replaceWith("<li>" + $(this).html() + "</li>");
});
而且请提高你的录取率。
答
可以使用的
http://api.jquery.com/replaceWith/
和
http://api.jquery.com/wrapAll/
$(".category p").wrapAll("<ul>").replaceWith(function() {
return $("<li></li>").append($(this).html());
});
提示的组合:有没有需要有明确的元素('section.clear'),只是使用其中一种简单的clearfix解决方案(在父级上是'overflow:hidden;'就足够了)。另外,如果你坚持使用html元素来清除浮动元素,为什么要使用语义'section'元素? 'section'不是在这里去''div',它在这里为我们过去使用div的一些东西提供了意义。然而,在清除元素的情况下,'section'不适合,'div'是。 – powerbuoy 2012-02-23 09:58:07