如何替换'< p>'与“< li>"在jQuery数组中?
问题描述:
假设我有这个
$(document).ready(function() {
var array = $.makeArray($('p'));
$(array).appendTo(document.body);
});
});
<p>how</p>
<p>are</p>
<p>you</p>
<p>baby?</p>
如果我想将<p>
替换为<li>
,并且预期的输出是...
If I want to replace <p>
with <li>
and expected output is ...
<li>how</li>
<li>are</li>
<li>you</li>
<li>baby?</li>
我该怎么办?预先感谢!
What should I do? Thanks in advance!
答
$("p").each(function () {
$(this).replaceWith("<li>" + $(this).html() + "</li>");
});