如何替换'<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>
如果我想用 替换
并且预期输出是 ...
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>");
});